5f90993426fa2bdc3b3d994c9e85e0805bb92bbcSimo Sorce Mmap Cache Common header
5f90993426fa2bdc3b3d994c9e85e0805bb92bbcSimo Sorce Copyright (C) Simo Sorce <ssorce@redhat.com> 2011
5f90993426fa2bdc3b3d994c9e85e0805bb92bbcSimo Sorce This program is free software; you can redistribute it and/or modify
5f90993426fa2bdc3b3d994c9e85e0805bb92bbcSimo Sorce it under the terms of the GNU General Public License as published by
5f90993426fa2bdc3b3d994c9e85e0805bb92bbcSimo Sorce the Free Software Foundation; either version 3 of the License, or
5f90993426fa2bdc3b3d994c9e85e0805bb92bbcSimo Sorce (at your option) any later version.
5f90993426fa2bdc3b3d994c9e85e0805bb92bbcSimo Sorce This program is distributed in the hope that it will be useful,
5f90993426fa2bdc3b3d994c9e85e0805bb92bbcSimo Sorce but WITHOUT ANY WARRANTY; without even the implied warranty of
5f90993426fa2bdc3b3d994c9e85e0805bb92bbcSimo Sorce MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5f90993426fa2bdc3b3d994c9e85e0805bb92bbcSimo Sorce GNU General Public License for more details.
5f90993426fa2bdc3b3d994c9e85e0805bb92bbcSimo Sorce You should have received a copy of the GNU General Public License
5f90993426fa2bdc3b3d994c9e85e0805bb92bbcSimo Sorce along with this program. If not, see <http://www.gnu.org/licenses/>.
5f90993426fa2bdc3b3d994c9e85e0805bb92bbcSimo Sorce/* NOTE: all the code here assumes that writing a uint32_t nto mmapped
5f90993426fa2bdc3b3d994c9e85e0805bb92bbcSimo Sorce * memory is an atomic operation and can't be split in multiple
5f90993426fa2bdc3b3d994c9e85e0805bb92bbcSimo Sorce * non-atomic operations */
5f90993426fa2bdc3b3d994c9e85e0805bb92bbcSimo Sorce/* align macros */
5f90993426fa2bdc3b3d994c9e85e0805bb92bbcSimo Sorce#define MC_ALIGN32(size) ( ((size) + MC_32 -1) & (~(MC_32 -1)) )
5f90993426fa2bdc3b3d994c9e85e0805bb92bbcSimo Sorce#define MC_ALIGN64(size) ( ((size) + MC_64 -1) & (~(MC_64 -1)) )
5f90993426fa2bdc3b3d994c9e85e0805bb92bbcSimo Sorce#define MC_HEADER_SIZE MC_ALIGN64(sizeof(struct sss_mc_header))
5f90993426fa2bdc3b3d994c9e85e0805bb92bbcSimo Sorce#define MC_DT_SIZE(elems, payload) ( (elems) * (payload) )
5f90993426fa2bdc3b3d994c9e85e0805bb92bbcSimo Sorce/* ^^ 8 bits per byte so we need just elems/8 bytes to represent all blocks */
5f90993426fa2bdc3b3d994c9e85e0805bb92bbcSimo Sorce#define MC_PTR_ADD(ptr, bytes) (void *)((uint8_t *)(ptr) + (bytes))
5f90993426fa2bdc3b3d994c9e85e0805bb92bbcSimo Sorce#define MC_PTR_DIFF(ptr, base) ((uint8_t *)(ptr) - (uint8_t *)(base))
581de96fc30b7fe44070f17a8a73f3374d38d6ffLukas Slebodnik * 40 seem a good compromise for slot size
5f90993426fa2bdc3b3d994c9e85e0805bb92bbcSimo Sorce * 4 blocks are enough for the average passwd entry of 42 bytes
581de96fc30b7fe44070f17a8a73f3374d38d6ffLukas Slebodnik * passwd records have 84 bytes of overhead, 160 - 82 = 78 bytes
581de96fc30b7fe44070f17a8a73f3374d38d6ffLukas Slebodnik * 3 blocks can contain a very minimal entry, 120 - 82 = 38 bytes
5f90993426fa2bdc3b3d994c9e85e0805bb92bbcSimo Sorce * 3 blocks are enough for groups w/o users (private user groups)
581de96fc30b7fe44070f17a8a73f3374d38d6ffLukas Slebodnik * group records have 68 bytes of overhead, 120 - 66 = 54 bytes
5f90993426fa2bdc3b3d994c9e85e0805bb92bbcSimo Sorce#define MC_SIZE_TO_SLOTS(len) (((len) + (MC_SLOT_SIZE - 1)) / MC_SLOT_SIZE)
4869633dc87dadb2b9a114444d375c39703ac863Pavel Březina#define MC_PTR_TO_SLOT(base, ptr) (MC_PTR_DIFF(ptr, base) / MC_SLOT_SIZE)
5f90993426fa2bdc3b3d994c9e85e0805bb92bbcSimo Sorce#define MC_VALID_BARRIER(val) (((val) & 0xff000000) == 0xf0000000)
31c47cacc7f9453153e57319474909d23122883fPavel Březina ((rec)->len >= MC_HEADER_SIZE && (rec)->len != MC_INVALID_VAL32 \
9028706a00da1bc48547e74aa872c825ac15adb2Michal Zidek#define SSS_MC_HEADER_UNINIT 0 /* after ftruncate or before reset */
5f90993426fa2bdc3b3d994c9e85e0805bb92bbcSimo Sorce#define SSS_MC_HEADER_ALIVE 1 /* current and in use */
5f90993426fa2bdc3b3d994c9e85e0805bb92bbcSimo Sorce#define SSS_MC_HEADER_RECYCLED 2 /* file was recycled, reopen asap */
5f90993426fa2bdc3b3d994c9e85e0805bb92bbcSimo Sorce uint32_t seed; /* random seed used to avoid collision attacks */
5f90993426fa2bdc3b3d994c9e85e0805bb92bbcSimo Sorce rel_ptr_t data_table; /* data table pointer relative to mmap base */
5f90993426fa2bdc3b3d994c9e85e0805bb92bbcSimo Sorce rel_ptr_t free_table; /* free table pointer relative to mmap base */
5f90993426fa2bdc3b3d994c9e85e0805bb92bbcSimo Sorce rel_ptr_t hash_table; /* hash table pointer relative to mmap base */
5f90993426fa2bdc3b3d994c9e85e0805bb92bbcSimo Sorce rel_ptr_t reserved; /* reserved for future changes */
5f90993426fa2bdc3b3d994c9e85e0805bb92bbcSimo Sorce uint32_t len; /* total record length including record data */
5f90993426fa2bdc3b3d994c9e85e0805bb92bbcSimo Sorce uint64_t expire; /* record expiration time (cast to time_t) */
581de96fc30b7fe44070f17a8a73f3374d38d6ffLukas Slebodnik rel_ptr_t next1; /* ptr of next record rel to data_table */
581de96fc30b7fe44070f17a8a73f3374d38d6ffLukas Slebodnik /* next1 is related to hash1 */
581de96fc30b7fe44070f17a8a73f3374d38d6ffLukas Slebodnik rel_ptr_t next2; /* ptr of next record rel to data_table */
581de96fc30b7fe44070f17a8a73f3374d38d6ffLukas Slebodnik /* next2 is related to hash2 */
5f90993426fa2bdc3b3d994c9e85e0805bb92bbcSimo Sorce uint32_t hash1; /* val of first hash (usually name of record) */
5f90993426fa2bdc3b3d994c9e85e0805bb92bbcSimo Sorce uint32_t hash2; /* val of second hash (usually id of record) */
581de96fc30b7fe44070f17a8a73f3374d38d6ffLukas Slebodnik uint32_t padding; /* padding & reserved for future changes */
5f90993426fa2bdc3b3d994c9e85e0805bb92bbcSimo Sorce uint32_t b2; /* barrier 2 - 32 bytes mark, fits a slot */
5f90993426fa2bdc3b3d994c9e85e0805bb92bbcSimo Sorce rel_ptr_t name; /* ptr to name string, rel. to struct base addr */
5f90993426fa2bdc3b3d994c9e85e0805bb92bbcSimo Sorce char strs[0]; /* concatenation of all passwd strings, each
5f90993426fa2bdc3b3d994c9e85e0805bb92bbcSimo Sorce * string is zero terminated ordered as follows:
5f90993426fa2bdc3b3d994c9e85e0805bb92bbcSimo Sorce * name, passwd, gecos, dir, shell */
c3ef027218fe9a7d16a70ca9d2f53e3d995e369fSimo Sorce rel_ptr_t name; /* ptr to name string, rel. to struct base addr */
c3ef027218fe9a7d16a70ca9d2f53e3d995e369fSimo Sorce uint32_t members; /* number of members in strs */
c3ef027218fe9a7d16a70ca9d2f53e3d995e369fSimo Sorce char strs[0]; /* concatenation of all group strings, each
c3ef027218fe9a7d16a70ca9d2f53e3d995e369fSimo Sorce * string is zero terminated ordered as follows:
c3ef027218fe9a7d16a70ca9d2f53e3d995e369fSimo Sorce * name, passwd, member1, member2, ... */
dda0258705de7255e6ec54b7f9adbde83a220996Lukas Slebodnik rel_ptr_t unique_name; /* ptr to unique name string, rel. to struct base addr */
dda0258705de7255e6ec54b7f9adbde83a220996Lukas Slebodnik rel_ptr_t name; /* ptr to raw name string, rel. to struct base addr */
225dc6914cdc8920b02a129b98ece1ed97b99c03Lukas Slebodnik rel_ptr_t strs; /* ptr to concatenation of all strings */
225dc6914cdc8920b02a129b98ece1ed97b99c03Lukas Slebodnik uint32_t data_len; /* all initgroups data len */
dda0258705de7255e6ec54b7f9adbde83a220996Lukas Slebodnik * string with name and unique_name is stored
dda0258705de7255e6ec54b7f9adbde83a220996Lukas Slebodnik * after gids */
5f90993426fa2bdc3b3d994c9e85e0805bb92bbcSimo Sorce#endif /* _MMAP_CACHE_H_ */