Line data Source code
1 : /*------------------------------------------------------------------------- 2 : * 3 : * aio_target.c 4 : * AIO - Functionality related to executing IO for different targets 5 : * 6 : * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group 7 : * Portions Copyright (c) 1994, Regents of the University of California 8 : * 9 : * IDENTIFICATION 10 : * src/backend/storage/aio/aio_target.c 11 : * 12 : *------------------------------------------------------------------------- 13 : */ 14 : 15 : #include "postgres.h" 16 : 17 : #include "storage/aio.h" 18 : #include "storage/aio_internal.h" 19 : #include "storage/smgr.h" 20 : 21 : 22 : /* 23 : * Registry for entities that can be the target of AIO. 24 : */ 25 : static const PgAioTargetInfo *pgaio_target_info[] = { 26 : [PGAIO_TID_INVALID] = &(PgAioTargetInfo) { 27 : .name = "invalid", 28 : }, 29 : [PGAIO_TID_SMGR] = &aio_smgr_target_info, 30 : }; 31 : 32 : 33 : 34 : /* -------------------------------------------------------------------------------- 35 : * Public target related functions operating on IO Handles 36 : * -------------------------------------------------------------------------------- 37 : */ 38 : 39 : bool 40 0 : pgaio_io_has_target(PgAioHandle *ioh) 41 : { 42 0 : return ioh->target != PGAIO_TID_INVALID; 43 : } 44 : 45 : /* 46 : * Return the name for the target associated with the IO. Mostly useful for 47 : * debugging/logging. 48 : */ 49 : const char * 50 0 : pgaio_io_get_target_name(PgAioHandle *ioh) 51 : { 52 : Assert(ioh->target >= 0 && ioh->target < PGAIO_TID_COUNT); 53 : 54 0 : return pgaio_target_info[ioh->target]->name; 55 : } 56 : 57 : /* 58 : * Assign a target to the IO. 59 : * 60 : * This has to be called exactly once before pgaio_io_start_*() is called. 61 : */ 62 : void 63 2423296 : pgaio_io_set_target(PgAioHandle *ioh, PgAioTargetID targetid) 64 : { 65 : Assert(ioh->state == PGAIO_HS_HANDED_OUT); 66 : Assert(ioh->target == PGAIO_TID_INVALID); 67 : 68 2423296 : ioh->target = targetid; 69 2423296 : } 70 : 71 : PgAioTargetData * 72 7823486 : pgaio_io_get_target_data(PgAioHandle *ioh) 73 : { 74 7823486 : return &ioh->target_data; 75 : } 76 : 77 : /* 78 : * Return a stringified description of the IO's target. 79 : * 80 : * The string is localized and allocated in the current memory context. 81 : */ 82 : char * 83 0 : pgaio_io_get_target_description(PgAioHandle *ioh) 84 : { 85 0 : return pgaio_target_info[ioh->target]->describe_identity(&ioh->target_data); 86 : } 87 : 88 : 89 : 90 : /* -------------------------------------------------------------------------------- 91 : * Internal target related functions operating on IO Handles 92 : * -------------------------------------------------------------------------------- 93 : */ 94 : 95 : /* 96 : * Internal: Check if pgaio_io_reopen() is available for the IO. 97 : */ 98 : bool 99 1182350 : pgaio_io_can_reopen(PgAioHandle *ioh) 100 : { 101 1182350 : return pgaio_target_info[ioh->target]->reopen != NULL; 102 : } 103 : 104 : /* 105 : * Internal: Before executing an IO outside of the context of the process the 106 : * IO has been staged in, the file descriptor has to be reopened - any FD 107 : * referenced in the IO itself, won't be valid in the separate process. 108 : */ 109 : void 110 972766 : pgaio_io_reopen(PgAioHandle *ioh) 111 : { 112 : Assert(ioh->target >= 0 && ioh->target < PGAIO_TID_COUNT); 113 : Assert(ioh->op >= 0 && ioh->op < PGAIO_OP_COUNT); 114 : 115 972766 : pgaio_target_info[ioh->target]->reopen(ioh); 116 972766 : }