[19c6950a] | 1 | /** |
---|
[4d3017a] | 2 | * @file rtems/libio_.h |
---|
[21242c2] | 3 | * |
---|
| 4 | * This file is the libio internal interface. |
---|
[4d3017a] | 5 | */ |
---|
| 6 | |
---|
[07a3253d] | 7 | /* |
---|
[21242c2] | 8 | * COPYRIGHT (c) 1989-2011. |
---|
[07a3253d] | 9 | * On-Line Applications Research Corporation (OAR). |
---|
| 10 | * |
---|
[3b7c123] | 11 | * Modifications to support reference counting in the file system are |
---|
| 12 | * Copyright (c) 2012 embedded brains GmbH. |
---|
| 13 | * |
---|
[07a3253d] | 14 | * The license and distribution terms for this file may be |
---|
| 15 | * found in the file LICENSE in this distribution or at |
---|
[0eae36c7] | 16 | * http://www.rtems.com/license/LICENSE. |
---|
[07a3253d] | 17 | * |
---|
| 18 | * $Id$ |
---|
| 19 | */ |
---|
| 20 | |
---|
[7945944] | 21 | #ifndef _RTEMS_RTEMS_LIBIO__H |
---|
| 22 | #define _RTEMS_RTEMS_LIBIO__H |
---|
[07a3253d] | 23 | |
---|
| 24 | #include <errno.h> |
---|
| 25 | |
---|
[3b7c123] | 26 | #include <rtems.h> |
---|
| 27 | #include <rtems/libio.h> |
---|
| 28 | #include <rtems/seterr.h> |
---|
| 29 | |
---|
[529ab915] | 30 | #ifdef __cplusplus |
---|
| 31 | extern "C" { |
---|
| 32 | #endif |
---|
| 33 | |
---|
[3b7c123] | 34 | #define RTEMS_FILESYSTEM_SYMLOOP_MAX 32 |
---|
| 35 | |
---|
[07a3253d] | 36 | /* |
---|
| 37 | * Semaphore to protect the io table |
---|
| 38 | */ |
---|
| 39 | |
---|
| 40 | #define RTEMS_LIBIO_SEM rtems_build_name('L', 'B', 'I', 'O') |
---|
| 41 | #define RTEMS_LIBIO_IOP_SEM(n) rtems_build_name('L', 'B', 'I', n) |
---|
| 42 | |
---|
[51435fc7] | 43 | extern rtems_id rtems_libio_semaphore; |
---|
[07a3253d] | 44 | |
---|
| 45 | /* |
---|
| 46 | * File descriptor Table Information |
---|
| 47 | */ |
---|
| 48 | |
---|
[83c5fc1] | 49 | extern uint32_t rtems_libio_number_iops; |
---|
[07a3253d] | 50 | extern rtems_libio_t *rtems_libio_iops; |
---|
| 51 | extern rtems_libio_t *rtems_libio_last_iop; |
---|
[cca4400] | 52 | extern rtems_libio_t *rtems_libio_iop_freelist; |
---|
[07a3253d] | 53 | |
---|
[3b7c123] | 54 | extern const rtems_filesystem_file_handlers_r rtems_filesystem_null_handlers; |
---|
| 55 | |
---|
| 56 | extern rtems_filesystem_mount_table_entry_t rtems_filesystem_null_mt_entry; |
---|
| 57 | |
---|
| 58 | /** |
---|
| 59 | * @brief The global null location. |
---|
| 60 | * |
---|
| 61 | * Every operation and the open and fstat handlers of this location returns an |
---|
| 62 | * error status. The errno is not touched by these operations and handlers. |
---|
| 63 | * The purpose of this location is to deliver the error return status for a |
---|
| 64 | * previous error condition which must set the errno accordingly. |
---|
| 65 | * |
---|
| 66 | * The usage of this null location instead of the NULL pointer eliminates a lot |
---|
| 67 | * of branches. |
---|
| 68 | * |
---|
| 69 | * The user environment root and current directory are statically initialized |
---|
| 70 | * with the null location. Due to that all file system services are in a |
---|
| 71 | * defined state even if no root file system was mounted. |
---|
| 72 | */ |
---|
| 73 | extern rtems_filesystem_global_location_t rtems_filesystem_global_location_null; |
---|
| 74 | |
---|
[07a3253d] | 75 | /* |
---|
| 76 | * rtems_libio_iop |
---|
| 77 | * |
---|
| 78 | * Macro to return the file descriptor pointer. |
---|
| 79 | */ |
---|
| 80 | |
---|
| 81 | #define rtems_libio_iop(_fd) \ |
---|
[a5b6cdd] | 82 | ((((uint32_t)(_fd)) < rtems_libio_number_iops) ? \ |
---|
[07a3253d] | 83 | &rtems_libio_iops[_fd] : 0) |
---|
| 84 | |
---|
[de2ee43d] | 85 | /* |
---|
| 86 | * rtems_libio_iop_to_descriptor |
---|
| 87 | * |
---|
| 88 | * Macro to convert an internal file descriptor pointer (iop) into |
---|
| 89 | * the integer file descriptor used by the "section 2" system calls. |
---|
| 90 | */ |
---|
| 91 | |
---|
| 92 | #define rtems_libio_iop_to_descriptor(_iop) \ |
---|
| 93 | ((!(_iop)) ? -1 : (_iop - rtems_libio_iops)) |
---|
| 94 | |
---|
[50f32b11] | 95 | /* |
---|
[2d733c42] | 96 | * rtems_libio_check_is_open |
---|
[50f32b11] | 97 | * |
---|
[2d733c42] | 98 | * Macro to check if a file descriptor is actually open. |
---|
| 99 | */ |
---|
| 100 | |
---|
| 101 | #define rtems_libio_check_is_open(_iop) \ |
---|
| 102 | do { \ |
---|
| 103 | if (((_iop)->flags & LIBIO_FLAGS_OPEN) == 0) { \ |
---|
| 104 | errno = EBADF; \ |
---|
| 105 | return -1; \ |
---|
| 106 | } \ |
---|
| 107 | } while (0) |
---|
| 108 | |
---|
[07a3253d] | 109 | /* |
---|
| 110 | * rtems_libio_check_fd |
---|
| 111 | * |
---|
| 112 | * Macro to check if a file descriptor number is valid. |
---|
| 113 | */ |
---|
| 114 | |
---|
| 115 | #define rtems_libio_check_fd(_fd) \ |
---|
| 116 | do { \ |
---|
[b79a45e] | 117 | if ((uint32_t) (_fd) >= rtems_libio_number_iops) { \ |
---|
[07a3253d] | 118 | errno = EBADF; \ |
---|
| 119 | return -1; \ |
---|
| 120 | } \ |
---|
| 121 | } while (0) |
---|
| 122 | |
---|
| 123 | /* |
---|
[2d733c42] | 124 | * rtems_libio_check_buffer |
---|
[07a3253d] | 125 | * |
---|
| 126 | * Macro to check if a buffer pointer is valid. |
---|
| 127 | */ |
---|
| 128 | |
---|
| 129 | #define rtems_libio_check_buffer(_buffer) \ |
---|
| 130 | do { \ |
---|
| 131 | if ((_buffer) == 0) { \ |
---|
| 132 | errno = EINVAL; \ |
---|
| 133 | return -1; \ |
---|
| 134 | } \ |
---|
| 135 | } while (0) |
---|
| 136 | |
---|
| 137 | /* |
---|
| 138 | * rtems_libio_check_count |
---|
| 139 | * |
---|
| 140 | * Macro to check if a count or length is valid. |
---|
| 141 | */ |
---|
| 142 | |
---|
| 143 | #define rtems_libio_check_count(_count) \ |
---|
| 144 | do { \ |
---|
| 145 | if ((_count) == 0) { \ |
---|
| 146 | return 0; \ |
---|
| 147 | } \ |
---|
| 148 | } while (0) |
---|
| 149 | |
---|
| 150 | /* |
---|
[58f3fab] | 151 | * rtems_libio_check_permissions_with_error |
---|
[07a3253d] | 152 | * |
---|
| 153 | * Macro to check if a file descriptor is open for this operation. |
---|
[58f3fab] | 154 | * On failure, return the user specified error. |
---|
[07a3253d] | 155 | */ |
---|
| 156 | |
---|
[58f3fab] | 157 | #define rtems_libio_check_permissions_with_error(_iop, _flag, _errno) \ |
---|
[b79a45e] | 158 | do { \ |
---|
| 159 | if (((_iop)->flags & (_flag)) == 0) { \ |
---|
[58f3fab] | 160 | rtems_set_errno_and_return_minus_one( _errno ); \ |
---|
[b79a45e] | 161 | return -1; \ |
---|
| 162 | } \ |
---|
[07a3253d] | 163 | } while (0) |
---|
| 164 | |
---|
[58f3fab] | 165 | /* |
---|
| 166 | * rtems_libio_check_permissions |
---|
| 167 | * |
---|
| 168 | * Macro to check if a file descriptor is open for this operation. |
---|
| 169 | * On failure, return EINVAL |
---|
| 170 | */ |
---|
| 171 | |
---|
| 172 | #define rtems_libio_check_permissions(_iop, _flag) \ |
---|
| 173 | rtems_libio_check_permissions_with_error(_iop, _flag, EINVAL ) |
---|
| 174 | |
---|
[3b7c123] | 175 | /** |
---|
| 176 | * @brief Clones a node. |
---|
[dd0f326] | 177 | * |
---|
[3b7c123] | 178 | * The caller must hold the file system instance lock. |
---|
| 179 | * |
---|
| 180 | * @param[out] clone The cloned location. |
---|
| 181 | * @param[in] master The master location. |
---|
| 182 | * |
---|
| 183 | * @see rtems_filesystem_instance_lock(). |
---|
[dd0f326] | 184 | */ |
---|
[3b7c123] | 185 | void rtems_filesystem_location_clone( |
---|
| 186 | rtems_filesystem_location_info_t *clone, |
---|
| 187 | const rtems_filesystem_location_info_t *master |
---|
| 188 | ); |
---|
[dd0f326] | 189 | |
---|
[3b7c123] | 190 | /** |
---|
| 191 | * @brief Returns the type of a node. |
---|
| 192 | * |
---|
| 193 | * This function obtains and releases the file system instance lock. |
---|
| 194 | * |
---|
| 195 | * @param[in] loc The location of the node. |
---|
| 196 | * |
---|
| 197 | * @return The node type. |
---|
| 198 | * |
---|
| 199 | * @see rtems_filesystem_instance_lock(). |
---|
[07a3253d] | 200 | */ |
---|
[3b7c123] | 201 | rtems_filesystem_node_types_t rtems_filesystem_node_type( |
---|
| 202 | const rtems_filesystem_location_info_t *loc |
---|
| 203 | ); |
---|
[2a929cc] | 204 | |
---|
[3b7c123] | 205 | /** |
---|
| 206 | * @brief Releases all resources of a location. |
---|
| 207 | * |
---|
| 208 | * This function may block on a mutex and may complete an unmount process. |
---|
| 209 | * |
---|
| 210 | * @param[in] loc The location to free. |
---|
| 211 | * |
---|
| 212 | * @note The file system root location is released by the file system instance |
---|
| 213 | * destruction handler (see @ref rtems_filesystem_fsunmount_me_t). |
---|
| 214 | * |
---|
| 215 | * @see rtems_filesystem_freenode_t. |
---|
| 216 | */ |
---|
| 217 | void rtems_filesystem_location_free( rtems_filesystem_location_info_t *loc ); |
---|
[2a929cc] | 218 | |
---|
| 219 | /* |
---|
[3b7c123] | 220 | * External structures |
---|
[2a929cc] | 221 | */ |
---|
[3b7c123] | 222 | #include <rtems/userenv.h> |
---|
[50f32b11] | 223 | |
---|
[068a824] | 224 | static inline void rtems_libio_lock( void ) |
---|
| 225 | { |
---|
| 226 | rtems_semaphore_obtain( rtems_libio_semaphore, RTEMS_WAIT, RTEMS_NO_TIMEOUT ); |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | static inline void rtems_libio_unlock( void ) |
---|
| 230 | { |
---|
| 231 | rtems_semaphore_release( rtems_libio_semaphore ); |
---|
| 232 | } |
---|
| 233 | |
---|
[3b7c123] | 234 | static inline void rtems_filesystem_mt_lock( void ) |
---|
| 235 | { |
---|
| 236 | rtems_libio_lock(); |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | static inline void rtems_filesystem_mt_unlock( void ) |
---|
| 240 | { |
---|
| 241 | rtems_libio_unlock(); |
---|
| 242 | } |
---|
| 243 | |
---|
| 244 | #define rtems_filesystem_mt_entry_declare_lock_context( ctx ) \ |
---|
| 245 | rtems_interrupt_level ctx |
---|
| 246 | |
---|
| 247 | #define rtems_filesystem_mt_entry_lock( ctx ) rtems_interrupt_disable( ctx ) |
---|
| 248 | |
---|
| 249 | #define rtems_filesystem_mt_entry_unlock( ctx ) rtems_interrupt_enable( ctx ) |
---|
| 250 | |
---|
| 251 | static inline void rtems_filesystem_instance_lock( |
---|
| 252 | const rtems_filesystem_location_info_t *loc |
---|
| 253 | ) |
---|
| 254 | { |
---|
| 255 | (*loc->ops->lock_h)( loc->mt_entry ); |
---|
| 256 | } |
---|
| 257 | |
---|
| 258 | static inline void rtems_filesystem_instance_unlock( |
---|
| 259 | const rtems_filesystem_location_info_t *loc |
---|
| 260 | ) |
---|
| 261 | { |
---|
| 262 | (*loc->ops->unlock_h)( loc->mt_entry ); |
---|
| 263 | } |
---|
| 264 | |
---|
[07a3253d] | 265 | /* |
---|
| 266 | * File Descriptor Routine Prototypes |
---|
| 267 | */ |
---|
| 268 | |
---|
| 269 | rtems_libio_t *rtems_libio_allocate(void); |
---|
| 270 | |
---|
[3b7c123] | 271 | uint32_t rtems_libio_fcntl_flags( int fcntl_flags ); |
---|
[07a3253d] | 272 | |
---|
[3b7c123] | 273 | int rtems_libio_to_fcntl_flags( uint32_t flags ); |
---|
[3ef8798] | 274 | |
---|
[07a3253d] | 275 | void rtems_libio_free( |
---|
| 276 | rtems_libio_t *iop |
---|
| 277 | ); |
---|
| 278 | |
---|
[3b7c123] | 279 | /* |
---|
| 280 | * File System Routine Prototypes |
---|
| 281 | */ |
---|
| 282 | |
---|
| 283 | rtems_filesystem_location_info_t * |
---|
| 284 | rtems_filesystem_eval_path_start( |
---|
| 285 | rtems_filesystem_eval_path_context_t *ctx, |
---|
| 286 | const char *path, |
---|
| 287 | int eval_flags |
---|
[07a3253d] | 288 | ); |
---|
| 289 | |
---|
[3b7c123] | 290 | rtems_filesystem_location_info_t * |
---|
| 291 | rtems_filesystem_eval_path_start_with_parent( |
---|
| 292 | rtems_filesystem_eval_path_context_t *ctx, |
---|
| 293 | const char *path, |
---|
| 294 | int eval_flags, |
---|
| 295 | rtems_filesystem_location_info_t *parentloc, |
---|
| 296 | int parent_eval_flags |
---|
[07a3253d] | 297 | ); |
---|
| 298 | |
---|
[3b7c123] | 299 | rtems_filesystem_location_info_t * |
---|
| 300 | rtems_filesystem_eval_path_start_with_root_and_current( |
---|
| 301 | rtems_filesystem_eval_path_context_t *ctx, |
---|
| 302 | const char *path, |
---|
| 303 | int eval_flags, |
---|
| 304 | rtems_filesystem_global_location_t *const *global_root_ptr, |
---|
| 305 | rtems_filesystem_global_location_t *const *global_current_ptr |
---|
| 306 | ); |
---|
| 307 | |
---|
| 308 | void rtems_filesystem_eval_path_continue( |
---|
| 309 | rtems_filesystem_eval_path_context_t *ctx |
---|
| 310 | ); |
---|
[07a3253d] | 311 | |
---|
[3b7c123] | 312 | void rtems_filesystem_eval_path_cleanup( |
---|
| 313 | rtems_filesystem_eval_path_context_t *ctx |
---|
[07a3253d] | 314 | ); |
---|
| 315 | |
---|
[3b7c123] | 316 | void rtems_filesystem_eval_path_recursive( |
---|
| 317 | rtems_filesystem_eval_path_context_t *ctx, |
---|
| 318 | const char *path, |
---|
| 319 | size_t pathlen |
---|
[7baa484] | 320 | ); |
---|
| 321 | |
---|
[3b7c123] | 322 | void rtems_filesystem_eval_path_cleanup_with_parent( |
---|
| 323 | rtems_filesystem_eval_path_context_t *ctx, |
---|
| 324 | rtems_filesystem_location_info_t *parentloc |
---|
[7baa484] | 325 | ); |
---|
| 326 | |
---|
[3b7c123] | 327 | /** |
---|
| 328 | * @brief Requests a path evaluation restart. |
---|
| 329 | * |
---|
| 330 | * Sets the start and current location to the new start location. The caller |
---|
| 331 | * must terminate its current evaluation process. The path evaluation |
---|
| 332 | * continues in the next loop iteration within |
---|
| 333 | * rtems_filesystem_eval_path_continue(). This avoids recursive invokations. |
---|
| 334 | * The function obtains the new start location and clones it to set the new |
---|
| 335 | * current location. The previous start and current locations are released. |
---|
| 336 | * |
---|
| 337 | * @param[in, out] ctx The path evaluation context. |
---|
| 338 | * @param[in, out] newstartloc_ptr Pointer to new start location. |
---|
| 339 | */ |
---|
| 340 | void rtems_filesystem_eval_path_restart( |
---|
| 341 | rtems_filesystem_eval_path_context_t *ctx, |
---|
| 342 | rtems_filesystem_global_location_t **newstartloc_ptr |
---|
| 343 | ); |
---|
| 344 | |
---|
| 345 | typedef enum { |
---|
| 346 | RTEMS_FILESYSTEM_EVAL_PATH_GENERIC_CONTINUE, |
---|
| 347 | RTEMS_FILESYSTEM_EVAL_PATH_GENERIC_DONE, |
---|
| 348 | RTEMS_FILESYSTEM_EVAL_PATH_GENERIC_NO_ENTRY |
---|
| 349 | } rtems_filesystem_eval_path_generic_status; |
---|
| 350 | |
---|
| 351 | /** |
---|
| 352 | * @brief Tests if the current location is a directory. |
---|
| 353 | * |
---|
| 354 | * @param[in, out] ctx The path evaluation context. |
---|
| 355 | * @param[in, out] arg The handler argument. |
---|
| 356 | * |
---|
| 357 | * @retval true The current location is a directory. |
---|
| 358 | * @retval false Otherwise. |
---|
| 359 | * |
---|
| 360 | * @see rtems_filesystem_eval_path_generic(). |
---|
| 361 | */ |
---|
| 362 | typedef bool (*rtems_filesystem_eval_path_is_directory)( |
---|
| 363 | rtems_filesystem_eval_path_context_t *ctx, |
---|
| 364 | void *arg |
---|
| 365 | ); |
---|
| 366 | |
---|
| 367 | /** |
---|
| 368 | * @brief Evaluates a token. |
---|
| 369 | * |
---|
| 370 | * @param[in, out] ctx The path evaluation context. |
---|
| 371 | * @param[in, out] arg The handler argument. |
---|
| 372 | * @param[in] token The token contents. |
---|
| 373 | * @param[in] tokenlen The token length in characters. |
---|
| 374 | * |
---|
| 375 | * @retval status The generic path evaluation status. |
---|
| 376 | * |
---|
| 377 | * @see rtems_filesystem_eval_path_generic(). |
---|
| 378 | */ |
---|
| 379 | typedef rtems_filesystem_eval_path_generic_status |
---|
| 380 | (*rtems_filesystem_eval_path_eval_token)( |
---|
| 381 | rtems_filesystem_eval_path_context_t *ctx, |
---|
| 382 | void *arg, |
---|
| 383 | const char *token, |
---|
| 384 | size_t tokenlen |
---|
| 385 | ); |
---|
| 386 | |
---|
| 387 | typedef struct { |
---|
| 388 | rtems_filesystem_eval_path_is_directory is_directory; |
---|
| 389 | rtems_filesystem_eval_path_eval_token eval_token; |
---|
| 390 | } rtems_filesystem_eval_path_generic_config; |
---|
| 391 | |
---|
| 392 | void rtems_filesystem_eval_path_generic( |
---|
| 393 | rtems_filesystem_eval_path_context_t *ctx, |
---|
| 394 | void *arg, |
---|
| 395 | const rtems_filesystem_eval_path_generic_config *config |
---|
[de691e68] | 396 | ); |
---|
| 397 | |
---|
[df3021ad] | 398 | void rtems_filesystem_initialize(void); |
---|
[07a3253d] | 399 | |
---|
[3b7c123] | 400 | /** |
---|
| 401 | * @brief Copies a location. |
---|
| 402 | * |
---|
| 403 | * A bitwise copy is performed. The destination location will be added to the |
---|
| 404 | * corresponding mount entry. |
---|
| 405 | * |
---|
| 406 | * @param[out] dst The destination location. |
---|
| 407 | * @param[in] src The source location. |
---|
| 408 | * |
---|
| 409 | * @retval dst The destination location. |
---|
| 410 | * |
---|
| 411 | * @see rtems_filesystem_location_clone(). |
---|
| 412 | */ |
---|
| 413 | rtems_filesystem_location_info_t *rtems_filesystem_location_copy( |
---|
| 414 | rtems_filesystem_location_info_t *dst, |
---|
| 415 | const rtems_filesystem_location_info_t *src |
---|
| 416 | ); |
---|
| 417 | |
---|
| 418 | static inline rtems_filesystem_location_info_t * |
---|
| 419 | rtems_filesystem_location_initialize_to_null( |
---|
| 420 | rtems_filesystem_location_info_t *loc |
---|
| 421 | ) |
---|
| 422 | { |
---|
| 423 | return rtems_filesystem_location_copy( |
---|
| 424 | loc, |
---|
| 425 | &rtems_filesystem_global_location_null.location |
---|
| 426 | ); |
---|
| 427 | } |
---|
[b79a45e] | 428 | |
---|
[3b7c123] | 429 | rtems_filesystem_global_location_t * |
---|
| 430 | rtems_filesystem_location_transform_to_global( |
---|
| 431 | rtems_filesystem_location_info_t *loc |
---|
| 432 | ); |
---|
[b79a45e] | 433 | |
---|
[3b7c123] | 434 | /** |
---|
| 435 | * @brief Assigns a global file system location. |
---|
| 436 | * |
---|
| 437 | * @param[in, out] lhs_global_loc_ptr Pointer to the global left hand side file |
---|
| 438 | * system location. The current left hand side location will be released. |
---|
| 439 | * @param[in] rhs_global_loc The global right hand side file system location. |
---|
| 440 | */ |
---|
| 441 | void rtems_filesystem_global_location_assign( |
---|
| 442 | rtems_filesystem_global_location_t **lhs_global_loc_ptr, |
---|
| 443 | rtems_filesystem_global_location_t *rhs_global_loc |
---|
| 444 | ); |
---|
[b79a45e] | 445 | |
---|
[3b7c123] | 446 | /** |
---|
| 447 | * @brief Obtains a global file system location. |
---|
| 448 | * |
---|
| 449 | * Deferred releases will be processed in this function. |
---|
| 450 | * |
---|
| 451 | * This function must be called from normal thread context and may block on a |
---|
| 452 | * mutex. Thread dispatching is disabled to protect some critical sections. |
---|
| 453 | * |
---|
| 454 | * @param[in] global_loc_ptr Pointer to the global file system location. |
---|
| 455 | * |
---|
| 456 | * @return A global file system location. It returns always a valid object. |
---|
| 457 | * In case of an error, the global null location will be returned. Each |
---|
| 458 | * operation or handler of the null location returns an error status. The |
---|
| 459 | * errno indicates the error. The NULL pointer is never returned. |
---|
| 460 | * |
---|
| 461 | * @see rtems_filesystem_location_transform_to_global(), |
---|
| 462 | * rtems_filesystem_global_location_obtain_null(), and |
---|
| 463 | * rtems_filesystem_global_location_release(). |
---|
| 464 | */ |
---|
| 465 | rtems_filesystem_global_location_t *rtems_filesystem_global_location_obtain( |
---|
| 466 | rtems_filesystem_global_location_t *const *global_loc_ptr |
---|
| 467 | ); |
---|
| 468 | |
---|
| 469 | /** |
---|
| 470 | * @brief Releases a global file system location. |
---|
| 471 | * |
---|
| 472 | * In case the reference count reaches zero, all associated resources will be |
---|
| 473 | * released. This may include the complete unmount of the corresponding file |
---|
| 474 | * system instance. |
---|
| 475 | * |
---|
| 476 | * This function may block on a mutex. It may be called within critical |
---|
| 477 | * sections of the operating system. In this case the release will be |
---|
| 478 | * deferred. The next obtain call will do the actual release. |
---|
| 479 | * |
---|
| 480 | * @param[in] global_loc The global file system location. It must not be NULL. |
---|
| 481 | * |
---|
| 482 | * @see rtems_filesystem_global_location_obtain(). |
---|
| 483 | */ |
---|
| 484 | void rtems_filesystem_global_location_release( |
---|
| 485 | rtems_filesystem_global_location_t *global_loc |
---|
| 486 | ); |
---|
| 487 | |
---|
| 488 | void rtems_filesystem_location_detach( |
---|
| 489 | rtems_filesystem_location_info_t *detach |
---|
| 490 | ); |
---|
| 491 | |
---|
| 492 | void rtems_filesystem_location_copy_and_detach( |
---|
| 493 | rtems_filesystem_location_info_t *copy, |
---|
| 494 | rtems_filesystem_location_info_t *detach |
---|
| 495 | ); |
---|
| 496 | |
---|
| 497 | static inline rtems_filesystem_global_location_t * |
---|
| 498 | rtems_filesystem_global_location_obtain_null(void) |
---|
| 499 | { |
---|
| 500 | rtems_filesystem_global_location_t *global_loc = NULL; |
---|
[b79a45e] | 501 | |
---|
[3b7c123] | 502 | return rtems_filesystem_global_location_obtain( &global_loc ); |
---|
| 503 | } |
---|
| 504 | |
---|
| 505 | static inline bool rtems_filesystem_location_is_null( |
---|
| 506 | const rtems_filesystem_location_info_t *loc |
---|
| 507 | ) |
---|
| 508 | { |
---|
| 509 | return loc->handlers == &rtems_filesystem_null_handlers; |
---|
| 510 | } |
---|
| 511 | |
---|
| 512 | static inline bool rtems_filesystem_global_location_is_null( |
---|
| 513 | const rtems_filesystem_global_location_t *global_loc |
---|
| 514 | ) |
---|
| 515 | { |
---|
| 516 | return rtems_filesystem_location_is_null( &global_loc->location ); |
---|
| 517 | } |
---|
| 518 | |
---|
| 519 | static inline void rtems_filesystem_location_error( |
---|
| 520 | const rtems_filesystem_location_info_t *loc, |
---|
| 521 | int eno |
---|
| 522 | ) |
---|
| 523 | { |
---|
| 524 | if ( !rtems_filesystem_location_is_null( loc ) ) { |
---|
| 525 | errno = eno; |
---|
| 526 | } |
---|
| 527 | } |
---|
| 528 | |
---|
| 529 | int rtems_filesystem_mknod( |
---|
| 530 | const rtems_filesystem_location_info_t *parentloc, |
---|
| 531 | const char *name, |
---|
| 532 | size_t namelen, |
---|
| 533 | mode_t mode, |
---|
| 534 | dev_t dev |
---|
| 535 | ); |
---|
| 536 | |
---|
| 537 | int rtems_filesystem_chdir( rtems_filesystem_location_info_t *loc ); |
---|
| 538 | |
---|
| 539 | int rtems_filesystem_chown( |
---|
| 540 | const char *path, |
---|
| 541 | uid_t owner, |
---|
| 542 | gid_t group, |
---|
| 543 | int eval_follow_link |
---|
| 544 | ); |
---|
| 545 | |
---|
| 546 | static inline bool rtems_filesystem_is_ready_for_unmount( |
---|
| 547 | rtems_filesystem_mount_table_entry_t *mt_entry |
---|
| 548 | ) |
---|
| 549 | { |
---|
| 550 | bool ready = !mt_entry->mounted |
---|
| 551 | && rtems_chain_has_only_one_node( &mt_entry->location_chain ) |
---|
| 552 | && mt_entry->mt_fs_root->reference_count == 1; |
---|
| 553 | |
---|
| 554 | if ( ready ) { |
---|
| 555 | rtems_chain_initialize_empty( &mt_entry->location_chain ); |
---|
| 556 | } |
---|
| 557 | |
---|
| 558 | return ready; |
---|
| 559 | } |
---|
| 560 | |
---|
| 561 | static inline void rtems_filesystem_location_add_to_mt_entry( |
---|
| 562 | rtems_filesystem_location_info_t *loc |
---|
| 563 | ) |
---|
| 564 | { |
---|
| 565 | rtems_filesystem_mt_entry_declare_lock_context( lock_context ); |
---|
| 566 | |
---|
| 567 | rtems_filesystem_mt_entry_lock( lock_context ); |
---|
| 568 | rtems_chain_append_unprotected( |
---|
| 569 | &loc->mt_entry->location_chain, |
---|
| 570 | &loc->mt_entry_node |
---|
| 571 | ); |
---|
| 572 | rtems_filesystem_mt_entry_unlock( lock_context ); |
---|
| 573 | } |
---|
| 574 | |
---|
| 575 | void rtems_filesystem_location_remove_from_mt_entry( |
---|
| 576 | rtems_filesystem_location_info_t *loc |
---|
| 577 | ); |
---|
| 578 | |
---|
| 579 | void rtems_filesystem_do_unmount( |
---|
| 580 | rtems_filesystem_mount_table_entry_t *mt_entry |
---|
| 581 | ); |
---|
| 582 | |
---|
| 583 | static inline bool rtems_filesystem_location_is_root( |
---|
[a2900a8b] | 584 | const rtems_filesystem_location_info_t *loc |
---|
| 585 | ) |
---|
| 586 | { |
---|
[3b7c123] | 587 | return (*loc->ops->are_nodes_equal_h)( |
---|
| 588 | loc, |
---|
| 589 | &loc->mt_entry->mt_fs_root->location |
---|
| 590 | ); |
---|
| 591 | } |
---|
| 592 | |
---|
| 593 | static inline const char *rtems_filesystem_eval_path_get_path( |
---|
| 594 | rtems_filesystem_eval_path_context_t *ctx |
---|
| 595 | ) |
---|
| 596 | { |
---|
| 597 | return ctx->path; |
---|
| 598 | } |
---|
| 599 | |
---|
| 600 | static inline size_t rtems_filesystem_eval_path_get_pathlen( |
---|
| 601 | rtems_filesystem_eval_path_context_t *ctx |
---|
| 602 | ) |
---|
| 603 | { |
---|
| 604 | return ctx->pathlen; |
---|
| 605 | } |
---|
| 606 | |
---|
| 607 | static inline void rtems_filesystem_eval_path_set_path( |
---|
| 608 | rtems_filesystem_eval_path_context_t *ctx, |
---|
| 609 | const char *path, |
---|
| 610 | size_t pathlen |
---|
| 611 | ) |
---|
| 612 | { |
---|
| 613 | ctx->path = path; |
---|
| 614 | ctx->pathlen = pathlen; |
---|
| 615 | } |
---|
| 616 | |
---|
| 617 | static inline void rtems_filesystem_eval_path_clear_path( |
---|
| 618 | rtems_filesystem_eval_path_context_t *ctx |
---|
| 619 | ) |
---|
| 620 | { |
---|
| 621 | ctx->pathlen = 0; |
---|
| 622 | } |
---|
| 623 | |
---|
| 624 | static inline const char *rtems_filesystem_eval_path_get_token( |
---|
| 625 | rtems_filesystem_eval_path_context_t *ctx |
---|
| 626 | ) |
---|
| 627 | { |
---|
| 628 | return ctx->token; |
---|
| 629 | } |
---|
| 630 | |
---|
| 631 | static inline size_t rtems_filesystem_eval_path_get_tokenlen( |
---|
| 632 | rtems_filesystem_eval_path_context_t *ctx |
---|
| 633 | ) |
---|
| 634 | { |
---|
| 635 | return ctx->tokenlen; |
---|
| 636 | } |
---|
| 637 | |
---|
| 638 | static inline void rtems_filesystem_eval_path_set_token( |
---|
| 639 | rtems_filesystem_eval_path_context_t *ctx, |
---|
| 640 | const char *token, |
---|
| 641 | size_t tokenlen |
---|
| 642 | ) |
---|
| 643 | { |
---|
| 644 | ctx->token = token; |
---|
| 645 | ctx->tokenlen = tokenlen; |
---|
| 646 | } |
---|
| 647 | |
---|
| 648 | static inline void rtems_filesystem_eval_path_clear_token( |
---|
| 649 | rtems_filesystem_eval_path_context_t *ctx |
---|
| 650 | ) |
---|
| 651 | { |
---|
| 652 | ctx->tokenlen = 0; |
---|
| 653 | } |
---|
| 654 | |
---|
| 655 | static inline void rtems_filesystem_eval_path_put_back_token( |
---|
| 656 | rtems_filesystem_eval_path_context_t *ctx |
---|
| 657 | ) |
---|
| 658 | { |
---|
| 659 | size_t tokenlen = ctx->tokenlen; |
---|
| 660 | |
---|
| 661 | ctx->path -= tokenlen; |
---|
| 662 | ctx->pathlen += tokenlen; |
---|
| 663 | ctx->tokenlen = 0; |
---|
| 664 | } |
---|
| 665 | |
---|
| 666 | void rtems_filesystem_eval_path_eat_delimiter( |
---|
| 667 | rtems_filesystem_eval_path_context_t *ctx |
---|
| 668 | ); |
---|
| 669 | |
---|
| 670 | void rtems_filesystem_eval_path_next_token( |
---|
| 671 | rtems_filesystem_eval_path_context_t *ctx |
---|
| 672 | ); |
---|
| 673 | |
---|
| 674 | static inline void rtems_filesystem_eval_path_get_next_token( |
---|
| 675 | rtems_filesystem_eval_path_context_t *ctx, |
---|
| 676 | const char **token, |
---|
| 677 | size_t *tokenlen |
---|
| 678 | ) |
---|
| 679 | { |
---|
| 680 | rtems_filesystem_eval_path_next_token(ctx); |
---|
| 681 | *token = ctx->token; |
---|
| 682 | *tokenlen = ctx->tokenlen; |
---|
| 683 | } |
---|
| 684 | |
---|
| 685 | static inline rtems_filesystem_location_info_t * |
---|
| 686 | rtems_filesystem_eval_path_get_currentloc( |
---|
| 687 | rtems_filesystem_eval_path_context_t *ctx |
---|
| 688 | ) |
---|
| 689 | { |
---|
| 690 | return &ctx->currentloc; |
---|
| 691 | } |
---|
| 692 | |
---|
| 693 | static inline bool rtems_filesystem_eval_path_has_path( |
---|
| 694 | const rtems_filesystem_eval_path_context_t *ctx |
---|
| 695 | ) |
---|
| 696 | { |
---|
| 697 | return ctx->pathlen > 0; |
---|
| 698 | } |
---|
| 699 | |
---|
| 700 | static inline bool rtems_filesystem_eval_path_has_token( |
---|
| 701 | const rtems_filesystem_eval_path_context_t *ctx |
---|
| 702 | ) |
---|
| 703 | { |
---|
| 704 | return ctx->tokenlen > 0; |
---|
| 705 | } |
---|
| 706 | |
---|
| 707 | static inline int rtems_filesystem_eval_path_get_flags( |
---|
| 708 | const rtems_filesystem_eval_path_context_t *ctx |
---|
| 709 | ) |
---|
| 710 | { |
---|
| 711 | return ctx->flags; |
---|
| 712 | } |
---|
| 713 | |
---|
| 714 | static inline void rtems_filesystem_eval_path_set_flags( |
---|
| 715 | rtems_filesystem_eval_path_context_t *ctx, |
---|
| 716 | int flags |
---|
| 717 | ) |
---|
| 718 | { |
---|
| 719 | ctx->flags = flags; |
---|
| 720 | } |
---|
| 721 | |
---|
| 722 | static inline void rtems_filesystem_eval_path_clear_and_set_flags( |
---|
| 723 | rtems_filesystem_eval_path_context_t *ctx, |
---|
| 724 | int clear, |
---|
| 725 | int set |
---|
| 726 | ) |
---|
| 727 | { |
---|
| 728 | int flags = ctx->flags; |
---|
| 729 | |
---|
| 730 | flags &= ~clear; |
---|
| 731 | flags |= set; |
---|
| 732 | |
---|
| 733 | ctx->flags = flags; |
---|
| 734 | } |
---|
| 735 | |
---|
| 736 | static inline void rtems_filesystem_eval_path_extract_currentloc( |
---|
| 737 | rtems_filesystem_eval_path_context_t *ctx, |
---|
| 738 | rtems_filesystem_location_info_t *get |
---|
| 739 | ) |
---|
| 740 | { |
---|
| 741 | rtems_filesystem_location_copy_and_detach( |
---|
| 742 | get, |
---|
| 743 | &ctx->currentloc |
---|
| 744 | ); |
---|
| 745 | } |
---|
| 746 | |
---|
| 747 | void rtems_filesystem_eval_path_error( |
---|
| 748 | rtems_filesystem_eval_path_context_t *ctx, |
---|
| 749 | int eno |
---|
| 750 | ); |
---|
| 751 | |
---|
| 752 | /** |
---|
| 753 | * @brief Checks that the locations exist in the same file system instance. |
---|
| 754 | * |
---|
| 755 | * @retval 0 The locations exist and are in the same file system instance. |
---|
| 756 | * @retval -1 An error occured. The @c errno indicates the error. |
---|
| 757 | */ |
---|
| 758 | int rtems_filesystem_location_exists_in_same_fs_instance_as( |
---|
| 759 | const rtems_filesystem_location_info_t *a, |
---|
| 760 | const rtems_filesystem_location_info_t *b |
---|
| 761 | ); |
---|
| 762 | |
---|
| 763 | bool rtems_filesystem_check_access( |
---|
| 764 | int eval_flags, |
---|
| 765 | mode_t node_mode, |
---|
| 766 | uid_t node_uid, |
---|
| 767 | gid_t node_gid |
---|
| 768 | ); |
---|
| 769 | |
---|
| 770 | bool rtems_filesystem_eval_path_check_access( |
---|
| 771 | rtems_filesystem_eval_path_context_t *ctx, |
---|
| 772 | int eval_flags, |
---|
| 773 | mode_t node_mode, |
---|
| 774 | uid_t node_uid, |
---|
| 775 | gid_t node_gid |
---|
| 776 | ); |
---|
| 777 | |
---|
| 778 | static inline bool rtems_filesystem_is_delimiter(char c) |
---|
| 779 | { |
---|
| 780 | return c == '/' || c == '\\'; |
---|
| 781 | } |
---|
| 782 | |
---|
| 783 | static inline bool rtems_filesystem_is_current_directory( |
---|
| 784 | const char *token, |
---|
| 785 | size_t tokenlen |
---|
| 786 | ) |
---|
| 787 | { |
---|
| 788 | return tokenlen == 1 && token [0] == '.'; |
---|
| 789 | } |
---|
| 790 | |
---|
| 791 | static inline bool rtems_filesystem_is_parent_directory( |
---|
| 792 | const char *token, |
---|
| 793 | size_t tokenlen |
---|
| 794 | ) |
---|
| 795 | { |
---|
| 796 | return tokenlen == 2 && token [0] == '.' && token [1] == '.'; |
---|
[a2900a8b] | 797 | } |
---|
| 798 | |
---|
[07a3253d] | 799 | #ifdef __cplusplus |
---|
| 800 | } |
---|
| 801 | #endif |
---|
| 802 | |
---|
| 803 | #endif |
---|
| 804 | /* end of include file */ |
---|