diff --git a/libfuse/Makefile b/libfuse/Makefile index 61a1a11f..4f416282 100644 --- a/libfuse/Makefile +++ b/libfuse/Makefile @@ -43,7 +43,8 @@ SRC = \ lib/fuse_session.c \ lib/fuse_signals.c \ lib/helper.c \ - lib/mount.c + lib/mount.c \ + lib/xmem.c OBJS = $(SRC:lib/%.c=build/%.o) DEPS = $(SRC:lib/%.c=build/%.d) CFLAGS ?= \ diff --git a/libfuse/include/khash.h b/libfuse/include/khash.h new file mode 100644 index 00000000..58268768 --- /dev/null +++ b/libfuse/include/khash.h @@ -0,0 +1,627 @@ +/* The MIT License + + Copyright (c) 2008, 2009, 2011 by Attractive Chaos + + Permission is hereby granted, free of charge, to any person obtaining + a copy of this software and associated documentation files (the + "Software"), to deal in the Software without restriction, including + without limitation the rights to use, copy, modify, merge, publish, + distribute, sublicense, and/or sell copies of the Software, and to + permit persons to whom the Software is furnished to do so, subject to + the following conditions: + + The above copyright notice and this permission notice shall be + included in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. +*/ + +/* + An example: + + #include "khash.h" + KHASH_MAP_INIT_INT(32, char) + int main() { + int ret, is_missing; + khiter_t k; + khash_t(32) *h = kh_init(32); + k = kh_put(32, h, 5, &ret); + kh_value(h, k) = 10; + k = kh_get(32, h, 10); + is_missing = (k == kh_end(h)); + k = kh_get(32, h, 5); + kh_del(32, h, k); + for (k = kh_begin(h); k != kh_end(h); ++k) + if (kh_exist(h, k)) kh_value(h, k) = 1; + kh_destroy(32, h); + return 0; + } +*/ + +/* + 2013-05-02 (0.2.8): + + * Use quadratic probing. When the capacity is power of 2, stepping function + i*(i+1)/2 guarantees to traverse each bucket. It is better than double + hashing on cache performance and is more robust than linear probing. + + In theory, double hashing should be more robust than quadratic probing. + However, my implementation is probably not for large hash tables, because + the second hash function is closely tied to the first hash function, + which reduce the effectiveness of double hashing. + + Reference: http://research.cs.vt.edu/AVresearch/hashing/quadratic.php + + 2011-12-29 (0.2.7): + + * Minor code clean up; no actual effect. + + 2011-09-16 (0.2.6): + + * The capacity is a power of 2. This seems to dramatically improve the + speed for simple keys. Thank Zilong Tan for the suggestion. Reference: + + - http://code.google.com/p/ulib/ + - http://nothings.org/computer/judy/ + + * Allow to optionally use linear probing which usually has better + performance for random input. Double hashing is still the default as it + is more robust to certain non-random input. + + * Added Wang's integer hash function (not used by default). This hash + function is more robust to certain non-random input. + + 2011-02-14 (0.2.5): + + * Allow to declare global functions. + + 2009-09-26 (0.2.4): + + * Improve portability + + 2008-09-19 (0.2.3): + + * Corrected the example + * Improved interfaces + + 2008-09-11 (0.2.2): + + * Improved speed a little in kh_put() + + 2008-09-10 (0.2.1): + + * Added kh_clear() + * Fixed a compiling error + + 2008-09-02 (0.2.0): + + * Changed to token concatenation which increases flexibility. + + 2008-08-31 (0.1.2): + + * Fixed a bug in kh_get(), which has not been tested previously. + + 2008-08-31 (0.1.1): + + * Added destructor + */ + + +#ifndef __AC_KHASH_H +#define __AC_KHASH_H + +/*! + @header + + Generic hash table library. +*/ + +#define AC_VERSION_KHASH_H "0.2.8" + +#include +#include +#include + +/* compiler specific configuration */ + +#if UINT_MAX == 0xffffffffu +typedef unsigned int khint32_t; +#elif ULONG_MAX == 0xffffffffu +typedef unsigned long khint32_t; +#endif + +#if ULONG_MAX == ULLONG_MAX +typedef unsigned long khint64_t; +#else +typedef unsigned long long khint64_t; +#endif + +#ifndef kh_inline +#ifdef _MSC_VER +#define kh_inline __inline +#else +#define kh_inline inline +#endif +#endif /* kh_inline */ + +#ifndef klib_unused +#if (defined __clang__ && __clang_major__ >= 3) || (defined __GNUC__ && __GNUC__ >= 3) +#define klib_unused __attribute__ ((__unused__)) +#else +#define klib_unused +#endif +#endif /* klib_unused */ + +typedef khint32_t khint_t; +typedef khint_t khiter_t; + +#define __ac_isempty(flag, i) ((flag[i>>4]>>((i&0xfU)<<1))&2) +#define __ac_isdel(flag, i) ((flag[i>>4]>>((i&0xfU)<<1))&1) +#define __ac_iseither(flag, i) ((flag[i>>4]>>((i&0xfU)<<1))&3) +#define __ac_set_isdel_false(flag, i) (flag[i>>4]&=~(1ul<<((i&0xfU)<<1))) +#define __ac_set_isempty_false(flag, i) (flag[i>>4]&=~(2ul<<((i&0xfU)<<1))) +#define __ac_set_isboth_false(flag, i) (flag[i>>4]&=~(3ul<<((i&0xfU)<<1))) +#define __ac_set_isdel_true(flag, i) (flag[i>>4]|=1ul<<((i&0xfU)<<1)) + +#define __ac_fsize(m) ((m) < 16? 1 : (m)>>4) + +#ifndef kroundup32 +#define kroundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x)) +#endif + +#ifndef kcalloc +#define kcalloc(N,Z) calloc(N,Z) +#endif +#ifndef kmalloc +#define kmalloc(Z) malloc(Z) +#endif +#ifndef krealloc +#define krealloc(P,Z) realloc(P,Z) +#endif +#ifndef kfree +#define kfree(P) free(P) +#endif + +static const double __ac_HASH_UPPER = 0.77; + +#define __KHASH_TYPE(name, khkey_t, khval_t) \ + typedef struct kh_##name##_s { \ + khint_t n_buckets, size, n_occupied, upper_bound; \ + khint32_t *flags; \ + khkey_t *keys; \ + khval_t *vals; \ + } kh_##name##_t; + +#define __KHASH_PROTOTYPES(name, khkey_t, khval_t) \ + extern kh_##name##_t *kh_init_##name(void); \ + extern void kh_destroy_##name(kh_##name##_t *h); \ + extern void kh_clear_##name(kh_##name##_t *h); \ + extern khint_t kh_get_##name(const kh_##name##_t *h, khkey_t key); \ + extern int kh_resize_##name(kh_##name##_t *h, khint_t new_n_buckets); \ + extern khint_t kh_put_##name(kh_##name##_t *h, khkey_t key, int *ret); \ + extern void kh_del_##name(kh_##name##_t *h, khint_t x); + +#define __KHASH_IMPL(name, SCOPE, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) \ + SCOPE kh_##name##_t *kh_init_##name(void) { \ + return (kh_##name##_t*)kcalloc(1, sizeof(kh_##name##_t)); \ + } \ + SCOPE void kh_destroy_##name(kh_##name##_t *h) \ + { \ + if (h) { \ + kfree((void *)h->keys); kfree(h->flags); \ + kfree((void *)h->vals); \ + kfree(h); \ + } \ + } \ + SCOPE void kh_clear_##name(kh_##name##_t *h) \ + { \ + if (h && h->flags) { \ + memset(h->flags, 0xaa, __ac_fsize(h->n_buckets) * sizeof(khint32_t)); \ + h->size = h->n_occupied = 0; \ + } \ + } \ + SCOPE khint_t kh_get_##name(const kh_##name##_t *h, khkey_t key) \ + { \ + if (h->n_buckets) { \ + khint_t k, i, last, mask, step = 0; \ + mask = h->n_buckets - 1; \ + k = __hash_func(key); i = k & mask; \ + last = i; \ + while (!__ac_isempty(h->flags, i) && (__ac_isdel(h->flags, i) || !__hash_equal(h->keys[i], key))) { \ + i = (i + (++step)) & mask; \ + if (i == last) return h->n_buckets; \ + } \ + return __ac_iseither(h->flags, i)? h->n_buckets : i; \ + } else return 0; \ + } \ + SCOPE int kh_resize_##name(kh_##name##_t *h, khint_t new_n_buckets) \ + { /* This function uses 0.25*n_buckets bytes of working space instead of [sizeof(key_t+val_t)+.25]*n_buckets. */ \ + khint32_t *new_flags = 0; \ + khint_t j = 1; \ + { \ + kroundup32(new_n_buckets); \ + if (new_n_buckets < 4) new_n_buckets = 4; \ + if (h->size >= (khint_t)(new_n_buckets * __ac_HASH_UPPER + 0.5)) j = 0; /* requested size is too small */ \ + else { /* hash table size to be changed (shrink or expand); rehash */ \ + new_flags = (khint32_t*)kmalloc(__ac_fsize(new_n_buckets) * sizeof(khint32_t)); \ + if (!new_flags) return -1; \ + memset(new_flags, 0xaa, __ac_fsize(new_n_buckets) * sizeof(khint32_t)); \ + if (h->n_buckets < new_n_buckets) { /* expand */ \ + khkey_t *new_keys = (khkey_t*)krealloc((void *)h->keys, new_n_buckets * sizeof(khkey_t)); \ + if (!new_keys) { kfree(new_flags); return -1; } \ + h->keys = new_keys; \ + if (kh_is_map) { \ + khval_t *new_vals = (khval_t*)krealloc((void *)h->vals, new_n_buckets * sizeof(khval_t)); \ + if (!new_vals) { kfree(new_flags); return -1; } \ + h->vals = new_vals; \ + } \ + } /* otherwise shrink */ \ + } \ + } \ + if (j) { /* rehashing is needed */ \ + for (j = 0; j != h->n_buckets; ++j) { \ + if (__ac_iseither(h->flags, j) == 0) { \ + khkey_t key = h->keys[j]; \ + khval_t val; \ + khint_t new_mask; \ + new_mask = new_n_buckets - 1; \ + if (kh_is_map) val = h->vals[j]; \ + __ac_set_isdel_true(h->flags, j); \ + while (1) { /* kick-out process; sort of like in Cuckoo hashing */ \ + khint_t k, i, step = 0; \ + k = __hash_func(key); \ + i = k & new_mask; \ + while (!__ac_isempty(new_flags, i)) i = (i + (++step)) & new_mask; \ + __ac_set_isempty_false(new_flags, i); \ + if (i < h->n_buckets && __ac_iseither(h->flags, i) == 0) { /* kick out the existing element */ \ + { khkey_t tmp = h->keys[i]; h->keys[i] = key; key = tmp; } \ + if (kh_is_map) { khval_t tmp = h->vals[i]; h->vals[i] = val; val = tmp; } \ + __ac_set_isdel_true(h->flags, i); /* mark it as deleted in the old hash table */ \ + } else { /* write the element and jump out of the loop */ \ + h->keys[i] = key; \ + if (kh_is_map) h->vals[i] = val; \ + break; \ + } \ + } \ + } \ + } \ + if (h->n_buckets > new_n_buckets) { /* shrink the hash table */ \ + h->keys = (khkey_t*)krealloc((void *)h->keys, new_n_buckets * sizeof(khkey_t)); \ + if (kh_is_map) h->vals = (khval_t*)krealloc((void *)h->vals, new_n_buckets * sizeof(khval_t)); \ + } \ + kfree(h->flags); /* free the working space */ \ + h->flags = new_flags; \ + h->n_buckets = new_n_buckets; \ + h->n_occupied = h->size; \ + h->upper_bound = (khint_t)(h->n_buckets * __ac_HASH_UPPER + 0.5); \ + } \ + return 0; \ + } \ + SCOPE khint_t kh_put_##name(kh_##name##_t *h, khkey_t key, int *ret) \ + { \ + khint_t x; \ + if (h->n_occupied >= h->upper_bound) { /* update the hash table */ \ + if (h->n_buckets > (h->size<<1)) { \ + if (kh_resize_##name(h, h->n_buckets - 1) < 0) { /* clear "deleted" elements */ \ + *ret = -1; return h->n_buckets; \ + } \ + } else if (kh_resize_##name(h, h->n_buckets + 1) < 0) { /* expand the hash table */ \ + *ret = -1; return h->n_buckets; \ + } \ + } /* TODO: to implement automatically shrinking; resize() already support shrinking */ \ + { \ + khint_t k, i, site, last, mask = h->n_buckets - 1, step = 0; \ + x = site = h->n_buckets; k = __hash_func(key); i = k & mask; \ + if (__ac_isempty(h->flags, i)) x = i; /* for speed up */ \ + else { \ + last = i; \ + while (!__ac_isempty(h->flags, i) && (__ac_isdel(h->flags, i) || !__hash_equal(h->keys[i], key))) { \ + if (__ac_isdel(h->flags, i)) site = i; \ + i = (i + (++step)) & mask; \ + if (i == last) { x = site; break; } \ + } \ + if (x == h->n_buckets) { \ + if (__ac_isempty(h->flags, i) && site != h->n_buckets) x = site; \ + else x = i; \ + } \ + } \ + } \ + if (__ac_isempty(h->flags, x)) { /* not present at all */ \ + h->keys[x] = key; \ + __ac_set_isboth_false(h->flags, x); \ + ++h->size; ++h->n_occupied; \ + *ret = 1; \ + } else if (__ac_isdel(h->flags, x)) { /* deleted */ \ + h->keys[x] = key; \ + __ac_set_isboth_false(h->flags, x); \ + ++h->size; \ + *ret = 2; \ + } else *ret = 0; /* Don't touch h->keys[x] if present and not deleted */ \ + return x; \ + } \ + SCOPE void kh_del_##name(kh_##name##_t *h, khint_t x) \ + { \ + if (x != h->n_buckets && !__ac_iseither(h->flags, x)) { \ + __ac_set_isdel_true(h->flags, x); \ + --h->size; \ + } \ + } + +#define KHASH_DECLARE(name, khkey_t, khval_t) \ + __KHASH_TYPE(name, khkey_t, khval_t) \ + __KHASH_PROTOTYPES(name, khkey_t, khval_t) + +#define KHASH_INIT2(name, SCOPE, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) \ + __KHASH_TYPE(name, khkey_t, khval_t) \ + __KHASH_IMPL(name, SCOPE, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) + +#define KHASH_INIT(name, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) \ + KHASH_INIT2(name, static kh_inline klib_unused, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) + +/* --- BEGIN OF HASH FUNCTIONS --- */ + +/*! @function + @abstract Integer hash function + @param key The integer [khint32_t] + @return The hash value [khint_t] +*/ +#define kh_int_hash_func(key) (khint32_t)(key) +/*! @function + @abstract Integer comparison function +*/ +#define kh_int_hash_equal(a, b) ((a) == (b)) +/*! @function + @abstract 64-bit integer hash function + @param key The integer [khint64_t] + @return The hash value [khint_t] +*/ +#define kh_int64_hash_func(key) (khint32_t)((key)>>33^(key)^(key)<<11) +/*! @function + @abstract 64-bit integer comparison function +*/ +#define kh_int64_hash_equal(a, b) ((a) == (b)) +/*! @function + @abstract const char* hash function + @param s Pointer to a null terminated string + @return The hash value +*/ +static kh_inline khint_t __ac_X31_hash_string(const char *s) +{ + khint_t h = (khint_t)*s; + if (h) for (++s ; *s; ++s) h = (h << 5) - h + (khint_t)*s; + return h; +} +/*! @function + @abstract Another interface to const char* hash function + @param key Pointer to a null terminated string [const char*] + @return The hash value [khint_t] +*/ +#define kh_str_hash_func(key) __ac_X31_hash_string(key) +/*! @function + @abstract Const char* comparison function +*/ +#define kh_str_hash_equal(a, b) (strcmp(a, b) == 0) + +static kh_inline khint_t __ac_Wang_hash(khint_t key) +{ + key += ~(key << 15); + key ^= (key >> 10); + key += (key << 3); + key ^= (key >> 6); + key += ~(key << 11); + key ^= (key >> 16); + return key; +} +#define kh_int_hash_func2(key) __ac_Wang_hash((khint_t)key) + +/* --- END OF HASH FUNCTIONS --- */ + +/* Other convenient macros... */ + +/*! + @abstract Type of the hash table. + @param name Name of the hash table [symbol] +*/ +#define khash_t(name) kh_##name##_t + +/*! @function + @abstract Initiate a hash table. + @param name Name of the hash table [symbol] + @return Pointer to the hash table [khash_t(name)*] +*/ +#define kh_init(name) kh_init_##name() + +/*! @function + @abstract Destroy a hash table. + @param name Name of the hash table [symbol] + @param h Pointer to the hash table [khash_t(name)*] +*/ +#define kh_destroy(name, h) kh_destroy_##name(h) + +/*! @function + @abstract Reset a hash table without deallocating memory. + @param name Name of the hash table [symbol] + @param h Pointer to the hash table [khash_t(name)*] +*/ +#define kh_clear(name, h) kh_clear_##name(h) + +/*! @function + @abstract Resize a hash table. + @param name Name of the hash table [symbol] + @param h Pointer to the hash table [khash_t(name)*] + @param s New size [khint_t] +*/ +#define kh_resize(name, h, s) kh_resize_##name(h, s) + +/*! @function + @abstract Insert a key to the hash table. + @param name Name of the hash table [symbol] + @param h Pointer to the hash table [khash_t(name)*] + @param k Key [type of keys] + @param r Extra return code: -1 if the operation failed; + 0 if the key is present in the hash table; + 1 if the bucket is empty (never used); 2 if the element in + the bucket has been deleted [int*] + @return Iterator to the inserted element [khint_t] +*/ +#define kh_put(name, h, k, r) kh_put_##name(h, k, r) + +/*! @function + @abstract Retrieve a key from the hash table. + @param name Name of the hash table [symbol] + @param h Pointer to the hash table [khash_t(name)*] + @param k Key [type of keys] + @return Iterator to the found element, or kh_end(h) if the element is absent [khint_t] +*/ +#define kh_get(name, h, k) kh_get_##name(h, k) + +/*! @function + @abstract Remove a key from the hash table. + @param name Name of the hash table [symbol] + @param h Pointer to the hash table [khash_t(name)*] + @param k Iterator to the element to be deleted [khint_t] +*/ +#define kh_del(name, h, k) kh_del_##name(h, k) + +/*! @function + @abstract Test whether a bucket contains data. + @param h Pointer to the hash table [khash_t(name)*] + @param x Iterator to the bucket [khint_t] + @return 1 if containing data; 0 otherwise [int] +*/ +#define kh_exist(h, x) (!__ac_iseither((h)->flags, (x))) + +/*! @function + @abstract Get key given an iterator + @param h Pointer to the hash table [khash_t(name)*] + @param x Iterator to the bucket [khint_t] + @return Key [type of keys] +*/ +#define kh_key(h, x) ((h)->keys[x]) + +/*! @function + @abstract Get value given an iterator + @param h Pointer to the hash table [khash_t(name)*] + @param x Iterator to the bucket [khint_t] + @return Value [type of values] + @discussion For hash sets, calling this results in segfault. +*/ +#define kh_val(h, x) ((h)->vals[x]) + +/*! @function + @abstract Alias of kh_val() +*/ +#define kh_value(h, x) ((h)->vals[x]) + +/*! @function + @abstract Get the start iterator + @param h Pointer to the hash table [khash_t(name)*] + @return The start iterator [khint_t] +*/ +#define kh_begin(h) (khint_t)(0) + +/*! @function + @abstract Get the end iterator + @param h Pointer to the hash table [khash_t(name)*] + @return The end iterator [khint_t] +*/ +#define kh_end(h) ((h)->n_buckets) + +/*! @function + @abstract Get the number of elements in the hash table + @param h Pointer to the hash table [khash_t(name)*] + @return Number of elements in the hash table [khint_t] +*/ +#define kh_size(h) ((h)->size) + +/*! @function + @abstract Get the number of buckets in the hash table + @param h Pointer to the hash table [khash_t(name)*] + @return Number of buckets in the hash table [khint_t] +*/ +#define kh_n_buckets(h) ((h)->n_buckets) + +/*! @function + @abstract Iterate over the entries in the hash table + @param h Pointer to the hash table [khash_t(name)*] + @param kvar Variable to which key will be assigned + @param vvar Variable to which value will be assigned + @param code Block of code to execute +*/ +#define kh_foreach(h, kvar, vvar, code) { khint_t __i; \ + for (__i = kh_begin(h); __i != kh_end(h); ++__i) { \ + if (!kh_exist(h,__i)) continue; \ + (kvar) = kh_key(h,__i); \ + (vvar) = kh_val(h,__i); \ + code; \ + } } + +/*! @function + @abstract Iterate over the values in the hash table + @param h Pointer to the hash table [khash_t(name)*] + @param vvar Variable to which value will be assigned + @param code Block of code to execute +*/ +#define kh_foreach_value(h, vvar, code) { khint_t __i; \ + for (__i = kh_begin(h); __i != kh_end(h); ++__i) { \ + if (!kh_exist(h,__i)) continue; \ + (vvar) = kh_val(h,__i); \ + code; \ + } } + +/* More convenient interfaces */ + +/*! @function + @abstract Instantiate a hash set containing integer keys + @param name Name of the hash table [symbol] +*/ +#define KHASH_SET_INIT_INT(name) \ + KHASH_INIT(name, khint32_t, char, 0, kh_int_hash_func, kh_int_hash_equal) + +/*! @function + @abstract Instantiate a hash map containing integer keys + @param name Name of the hash table [symbol] + @param khval_t Type of values [type] +*/ +#define KHASH_MAP_INIT_INT(name, khval_t) \ + KHASH_INIT(name, khint32_t, khval_t, 1, kh_int_hash_func, kh_int_hash_equal) + +/*! @function + @abstract Instantiate a hash set containing 64-bit integer keys + @param name Name of the hash table [symbol] +*/ +#define KHASH_SET_INIT_INT64(name) \ + KHASH_INIT(name, khint64_t, char, 0, kh_int64_hash_func, kh_int64_hash_equal) + +/*! @function + @abstract Instantiate a hash map containing 64-bit integer keys + @param name Name of the hash table [symbol] + @param khval_t Type of values [type] +*/ +#define KHASH_MAP_INIT_INT64(name, khval_t) \ + KHASH_INIT(name, khint64_t, khval_t, 1, kh_int64_hash_func, kh_int64_hash_equal) + +typedef const char *kh_cstr_t; +/*! @function + @abstract Instantiate a hash map containing const char* keys + @param name Name of the hash table [symbol] +*/ +#define KHASH_SET_INIT_STR(name) \ + KHASH_INIT(name, kh_cstr_t, char, 0, kh_str_hash_func, kh_str_hash_equal) + +/*! @function + @abstract Instantiate a hash map containing const char* keys + @param name Name of the hash table [symbol] + @param khval_t Type of values [type] +*/ +#define KHASH_MAP_INIT_STR(name, khval_t) \ + KHASH_INIT(name, kh_cstr_t, khval_t, 1, kh_str_hash_func, kh_str_hash_equal) + +#endif /* __AC_KHASH_H */ diff --git a/libfuse/include/xmem.h b/libfuse/include/xmem.h new file mode 100644 index 00000000..8b21d38b --- /dev/null +++ b/libfuse/include/xmem.h @@ -0,0 +1,46 @@ +/* + ISC License + + Copyright (c) 2020, Antonio SJ Musumeci + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#pragma once + +#include "extern_c.h" + +#include +#include + +EXTERN_C_BEGIN + +void xmem_enable(void); +void xmem_disable(void); + +void *xmem_malloc_(size_t size, const char *file, const int line); +void *xmem_realloc_(void *ptr, size_t size, const char *file, const int line); +void *xmem_calloc_(size_t nmemb, size_t size, const char *file, const int line); +void xmem_free_(void *ptr, const char *file, const int line); +char *xmem_strdup_(const char *s, const char *file, const int line); + +void xmem_print(FILE *file); +void xmem_print_to_tmp(void); + +#define xmem_malloc(SIZE) (xmem_malloc_((SIZE),__FILE__,__LINE__)) +#define xmem_realloc(PTR,SIZE) (xmem_realloc_((PTR),(SIZE),__FILE__,__LINE__)) +#define xmem_calloc(NMEMB,SIZE) (xmem_calloc_((NMEMB),(SIZE),__FILE__,__LINE__)) +#define xmem_free(PTR) (xmem_free_((PTR),__FILE__,__LINE__)) +#define xmem_strdup(STR) (xmem_strdup_((STR),__FILE__,__LINE__)) + +EXTERN_C_END diff --git a/libfuse/lib/fuse.c b/libfuse/lib/fuse.c index 8a53495b..35b7e3aa 100644 --- a/libfuse/lib/fuse.c +++ b/libfuse/lib/fuse.c @@ -17,6 +17,7 @@ #include "fuse_misc.h" #include "fuse_kernel.h" #include "fuse_dirents.h" +#include "xmem.h" #include #include @@ -39,12 +40,6 @@ #include #include -#define FUSE_NODE_SLAB 1 - -#ifndef MAP_ANONYMOUS -#undef FUSE_NODE_SLAB -#endif - #define FUSE_DEFAULT_INTR_SIGNAL SIGUSR1 #define FUSE_UNKNOWN_INO UINT64_MAX @@ -256,6 +251,43 @@ list_del(struct list_head *entry) prev->next = next; } +static +void +fuse_print_table(struct fuse *f_) +{ + FILE *f; + size_t i; + char filepath[256]; + + sprintf(filepath,"/tmp/mergerfs.nodes.%d.%ld",getpid(),time(NULL)); + f = fopen(filepath,"w+"); + if(f == NULL) + f = stderr; + + pthread_mutex_lock(&f_->lock); + for(i = 0; i < f_->id_table.size; i++) + { + struct node *node; + + for(node = f_->id_table.array[i]; node != NULL; node = node->id_next) + { + fprintf(f, + "id:%lu; pid:%lu; ref:%d; %s\n", + node->nodeid, + (node->parent ? node->parent->nodeid : 0), + node->refctr, + node->name); + } + } + pthread_mutex_unlock(&f_->lock); + + if(f != stderr) + { + fprintf(stderr,"%s\n",filepath); + fclose(f); + } +} + static inline int @@ -282,118 +314,11 @@ get_node_size(struct fuse *f) return sizeof(struct node); } -#ifdef FUSE_NODE_SLAB -static -struct node_slab* -list_to_slab(struct list_head *head) -{ - return (struct node_slab *) head; -} - -static -struct node_slab* -node_to_slab(struct fuse *f, struct node *node) -{ - return (struct node_slab *) (((uintptr_t) node) & ~((uintptr_t) f->pagesize - 1)); -} - -static -int -alloc_slab(struct fuse *f) -{ - void *mem; - struct node_slab *slab; - char *start; - size_t num; - size_t i; - size_t node_size = get_node_size(f); - - mem = mmap(NULL, f->pagesize, PROT_READ | PROT_WRITE, - MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); - - if (mem == MAP_FAILED) - return -1; - - slab = mem; - init_list_head(&slab->freelist); - slab->used = 0; - num = (f->pagesize - sizeof(struct node_slab)) / node_size; - - start = (char *) mem + f->pagesize - num * node_size; - for (i = 0; i < num; i++) { - struct list_head *n; - - n = (struct list_head *) (start + i * node_size); - list_add_tail(n, &slab->freelist); - } - list_add_tail(&slab->list, &f->partial_slabs); - - return 0; -} - static struct node* alloc_node(struct fuse *f) { - struct node_slab *slab; - struct list_head *node; - - if (list_empty(&f->partial_slabs)) { - int res = alloc_slab(f); - if (res != 0) - return NULL; - } - slab = list_to_slab(f->partial_slabs.next); - slab->used++; - node = slab->freelist.next; - list_del(node); - if (list_empty(&slab->freelist)) { - list_del(&slab->list); - list_add_tail(&slab->list, &f->full_slabs); - } - memset(node, 0, sizeof(struct node)); - - return (struct node *) node; -} - -static -void -free_slab(struct fuse *f, - struct node_slab *slab) -{ - int res; - - list_del(&slab->list); - res = munmap(slab, f->pagesize); - if (res == -1) - fprintf(stderr, "fuse warning: munmap(%p) failed\n", slab); -} - -static -void -free_node_mem(struct fuse *f, - struct node *node) -{ - struct node_slab *slab = node_to_slab(f, node); - struct list_head *n = (struct list_head *) node; - - slab->used--; - if (slab->used) { - if (list_empty(&slab->freelist)) { - list_del(&slab->list); - list_add_tail(&slab->list, &f->partial_slabs); - } - list_add_head(n, &slab->freelist); - } else { - free_slab(f, slab); - } -} -#else -static -struct node* -alloc_node(struct fuse *f) -{ - return (struct node *) calloc(1, get_node_size(f)); + return (struct node *)xmem_calloc(1, get_node_size(f)); } static @@ -402,9 +327,8 @@ free_node_mem(struct fuse *f, struct node *node) { (void) f; - free(node); + xmem_free(node); } -#endif static size_t @@ -483,7 +407,7 @@ free_node(struct fuse *f_, struct node *node_) { if(node_->name != node_->inline_name) - free(node_->name); + xmem_free(node_->name); if(node_->is_hidden) fuse_fs_free_hide(f_->fs,node_->hidden_fh); @@ -501,7 +425,7 @@ node_table_reduce(struct node_table *t) if (newsize < NODE_TABLE_MIN_SIZE) return; - newarray = realloc(t->array, sizeof(struct node *) * newsize); + newarray = xmem_realloc(t->array, sizeof(struct node *) * newsize); if (newarray != NULL) t->array = newarray; @@ -559,7 +483,7 @@ static int node_table_resize(struct node_table *t) size_t newsize = t->size * 2; void *newarray; - newarray = realloc(t->array, sizeof(struct node *) * newsize); + newarray = xmem_realloc(t->array, sizeof(struct node *) * newsize); if (newarray == NULL) return -1; @@ -668,7 +592,7 @@ static void unhash_name(struct fuse *f, struct node *node) node->name_next = NULL; unref_node(f, node->parent); if (node->name != node->inline_name) - free(node->name); + xmem_free(node->name); node->name = NULL; node->parent = NULL; f->name_table.use--; @@ -866,7 +790,7 @@ static char *add_name(char **buf, unsigned *bufsize, char *s, const char *name) newbufsize *= 2; } - newbuf = realloc(*buf, newbufsize); + newbuf = xmem_realloc(*buf, newbufsize); if (newbuf == NULL) return NULL; @@ -917,7 +841,7 @@ static int try_get_path(struct fuse *f, fuse_ino_t nodeid, const char *name, *path = NULL; err = -ENOMEM; - buf = malloc(bufsize); + buf = xmem_malloc(bufsize); if (buf == NULL) goto out_err; @@ -980,7 +904,7 @@ static int try_get_path(struct fuse *f, fuse_ino_t nodeid, const char *name, if (need_lock) unlock_path(f, nodeid, wnode, node); out_free: - free(buf); + xmem_free(buf); out_err: return err; @@ -1172,7 +1096,7 @@ static int try_get_path2(struct fuse *f, fuse_ino_t nodeid1, const char *name1, struct node *wn1 = wnode1 ? *wnode1 : NULL; unlock_path(f, nodeid1, wn1, NULL); - free(*path1); + xmem_free(*path1); } } return err; @@ -1219,7 +1143,7 @@ static void free_path_wrlock(struct fuse *f, fuse_ino_t nodeid, if (f->lockq) wake_up_queued(f); pthread_mutex_unlock(&f->lock); - free(path); + xmem_free(path); } static void free_path(struct fuse *f, fuse_ino_t nodeid, char *path) @@ -1237,8 +1161,8 @@ static void free_path2(struct fuse *f, fuse_ino_t nodeid1, fuse_ino_t nodeid2, unlock_path(f, nodeid2, wnode2, NULL); wake_up_queued(f); pthread_mutex_unlock(&f->lock); - free(path1); - free(path2); + xmem_free(path1); + xmem_free(path2); } static @@ -1316,15 +1240,14 @@ static int rename_node(struct fuse *f, fuse_ino_t olddir, const char *oldname, int err = 0; pthread_mutex_lock(&f->lock); - node = lookup_node(f, olddir, oldname); - newnode = lookup_node(f, newdir, newname); + node = lookup_node(f, olddir, oldname); + newnode = lookup_node(f, newdir, newname); if (node == NULL) goto out; if (newnode != NULL) unlink_node(f, newnode); - unhash_name(f, node); if (hash_name(f, node, newdir, newname) == -1) { err = -ENOMEM; @@ -1612,8 +1535,8 @@ static void fuse_free_buf(struct fuse_bufvec *buf) size_t i; for (i = 0; i < buf->count; i++) - free(buf->buf[i].mem); - free(buf); + xmem_free(buf->buf[i].mem); + xmem_free(buf); } } @@ -1637,13 +1560,13 @@ int fuse_fs_read_buf(struct fuse_fs *fs, struct fuse_bufvec *buf; void *mem; - buf = malloc(sizeof(struct fuse_bufvec)); + buf = xmem_malloc(sizeof(struct fuse_bufvec)); if (buf == NULL) return -ENOMEM; - mem = malloc(size); + mem = xmem_malloc(size); if (mem == NULL) { - free(buf); + xmem_free(buf); return -ENOMEM; } *buf = FUSE_BUFVEC_INIT(size); @@ -1721,7 +1644,7 @@ int fuse_fs_write_buf(struct fuse_fs *fs, flatbuf = &buf->buf[0]; } else { res = -ENOMEM; - mem = malloc(size); + mem = xmem_malloc(size); if (mem == NULL) goto out; @@ -1737,7 +1660,7 @@ int fuse_fs_write_buf(struct fuse_fs *fs, res = fs->op.write(flatbuf->mem, flatbuf->size, off, fi); out_free: - free(mem); + xmem_free(mem); } out: if (fs->debug && res >= 0) @@ -2335,7 +2258,7 @@ static struct fuse_context_i *fuse_get_context_internal(void) c = (struct fuse_context_i *) pthread_getspecific(fuse_context_key); if (c == NULL) { c = (struct fuse_context_i *) - calloc(1, sizeof(struct fuse_context_i)); + xmem_calloc(1, sizeof(struct fuse_context_i)); if (c == NULL) { /* This is hard to deal with properly, so just abort. If memory is so low that the @@ -2351,7 +2274,7 @@ static struct fuse_context_i *fuse_get_context_internal(void) static void fuse_freecontext(void *data) { - free(data); + xmem_free(data); } static int fuse_create_context_key(void) @@ -2377,7 +2300,7 @@ static void fuse_delete_context_key(void) pthread_mutex_lock(&fuse_context_lock); fuse_context_ref--; if (!fuse_context_ref) { - free(pthread_getspecific(fuse_context_key)); + xmem_free(pthread_getspecific(fuse_context_key)); pthread_key_delete(fuse_context_key); } pthread_mutex_unlock(&fuse_context_lock); @@ -2445,7 +2368,7 @@ void fuse_fs_destroy(struct fuse_fs *fs) fuse_get_context()->private_data = fs->user_data; if (fs->op.destroy) fs->op.destroy(fs->user_data); - free(fs); + xmem_free(fs); } static void fuse_lib_destroy(void *data) @@ -3258,7 +3181,7 @@ static void fuse_lib_opendir(fuse_req_t req, fuse_ino_t ino, char *path; int err; - dh = (struct fuse_dh *) calloc(1,sizeof(struct fuse_dh)); + dh = (struct fuse_dh *)xmem_calloc(1,sizeof(struct fuse_dh)); if (dh == NULL) { reply_err(req, -ENOMEM); return; @@ -3288,12 +3211,12 @@ static void fuse_lib_opendir(fuse_req_t req, fuse_ino_t ino, must be cancelled */ fuse_fs_releasedir(f->fs, &fi); pthread_mutex_destroy(&dh->lock); - free(dh); + xmem_free(dh); } } else { reply_err(req, err); pthread_mutex_destroy(&dh->lock); - free(dh); + xmem_free(dh); } free_path(f, ino, path); } @@ -3456,7 +3379,7 @@ fuse_lib_releasedir(fuse_req_t req_, pthread_mutex_unlock(&dh->lock); pthread_mutex_destroy(&dh->lock); fuse_dirents_free(&dh->d); - free(dh); + xmem_free(dh); reply_err(req_,0); } @@ -3544,7 +3467,7 @@ static void fuse_lib_getxattr(fuse_req_t req, fuse_ino_t ino, const char *name, int res; if (size) { - char *value = (char *) malloc(size); + char *value = (char*)xmem_malloc(size); if (value == NULL) { reply_err(req, -ENOMEM); return; @@ -3554,7 +3477,7 @@ static void fuse_lib_getxattr(fuse_req_t req, fuse_ino_t ino, const char *name, fuse_reply_buf(req, value, res); else reply_err(req, res); - free(value); + xmem_free(value); } else { res = common_getxattr(f, req, ino, name, NULL, 0); if (res >= 0) @@ -3587,7 +3510,7 @@ static void fuse_lib_listxattr(fuse_req_t req, fuse_ino_t ino, size_t size) int res; if (size) { - char *list = (char *) malloc(size); + char *list = (char*)xmem_malloc(size); if (list == NULL) { reply_err(req, -ENOMEM); return; @@ -3597,7 +3520,7 @@ static void fuse_lib_listxattr(fuse_req_t req, fuse_ino_t ino, size_t size) fuse_reply_buf(req, list, res); else reply_err(req, res); - free(list); + xmem_free(list); } else { res = common_listxattr(f, req, ino, NULL, 0); if (res >= 0) @@ -3678,7 +3601,7 @@ static void delete_lock(struct lock **lockp) { struct lock *l = *lockp; *lockp = l->next; - free(l); + xmem_free(l); } static void insert_lock(struct lock **pos, struct lock *lock) @@ -3695,12 +3618,12 @@ static int locks_insert(struct node *node, struct lock *lock) if (lock->type != F_UNLCK || lock->start != 0 || lock->end != OFFSET_MAX) { - newl1 = malloc(sizeof(struct lock)); - newl2 = malloc(sizeof(struct lock)); + newl1 = xmem_malloc(sizeof(struct lock)); + newl2 = xmem_malloc(sizeof(struct lock)); if (!newl1 || !newl2) { - free(newl1); - free(newl2); + xmem_free(newl1); + xmem_free(newl2); return -ENOLCK; } } @@ -3756,8 +3679,8 @@ static int locks_insert(struct node *node, struct lock *lock) newl1 = NULL; } out: - free(newl1); - free(newl2); + xmem_free(newl1); + xmem_free(newl2); return 0; } @@ -3960,7 +3883,7 @@ static void fuse_lib_ioctl(fuse_req_t req, fuse_ino_t ino, unsigned long cmd, vo if (out_bufsz) { err = -ENOMEM; - out_buf = malloc(out_bufsz); + out_buf = xmem_malloc(out_bufsz); if (!out_buf) goto err; } @@ -3976,12 +3899,18 @@ static void fuse_lib_ioctl(fuse_req_t req, fuse_ino_t ino, unsigned long cmd, vo fuse_finish_interrupt(f, req, &d); + if(err == -0xDEADBEEF) + { + fuse_print_table(f); + err = 0; + } + fuse_reply_ioctl(req, err, out_buf, out_bufsz); goto out; err: reply_err(req, err); out: - free(out_buf); + xmem_free(out_buf); } static void fuse_lib_poll(fuse_req_t req, fuse_ino_t ino, @@ -4122,8 +4051,8 @@ int fuse_notify_poll(struct fuse_pollhandle *ph) static void free_cmd(struct fuse_cmd *cmd) { - free(cmd->buf); - free(cmd); + xmem_free(cmd->buf); + xmem_free(cmd); } void fuse_process_cmd(struct fuse *f, struct fuse_cmd *cmd) @@ -4144,15 +4073,15 @@ struct fuse_session *fuse_get_session(struct fuse *f) static struct fuse_cmd *fuse_alloc_cmd(size_t bufsize) { - struct fuse_cmd *cmd = (struct fuse_cmd *) malloc(sizeof(*cmd)); + struct fuse_cmd *cmd = (struct fuse_cmd *)xmem_malloc(sizeof(*cmd)); if (cmd == NULL) { fprintf(stderr, "fuse: failed to allocate cmd\n"); return NULL; } - cmd->buf = (char *) malloc(bufsize); + cmd->buf = (char*)xmem_malloc(bufsize); if (cmd->buf == NULL) { fprintf(stderr, "fuse: failed to allocate read buffer\n"); - free(cmd); + xmem_free(cmd); return NULL; } return cmd; @@ -4314,7 +4243,7 @@ struct fuse_fs *fuse_fs_new(const struct fuse_operations *op, size_t op_size, op_size = sizeof(struct fuse_operations); } - fs = (struct fuse_fs *) calloc(1, sizeof(struct fuse_fs)); + fs = (struct fuse_fs *)xmem_calloc(1, sizeof(struct fuse_fs)); if (!fs) { fprintf(stderr, "fuse: failed to allocate fuse_fs object\n"); return NULL; @@ -4329,7 +4258,7 @@ struct fuse_fs *fuse_fs_new(const struct fuse_operations *op, size_t op_size, static int node_table_init(struct node_table *t) { t->size = NODE_TABLE_MIN_SIZE; - t->array = (struct node **) calloc(1, sizeof(struct node *) * t->size); + t->array = (struct node **)xmem_calloc(1, sizeof(struct node *) * t->size); if (t->array == NULL) { fprintf(stderr, "fuse: memory allocation failed\n"); return -1; @@ -4382,7 +4311,7 @@ struct fuse *fuse_new_common(struct fuse_chan *ch, struct fuse_args *args, if (fuse_create_context_key() == -1) goto out; - f = (struct fuse *) calloc(1, sizeof(struct fuse)); + f = (struct fuse *)xmem_calloc(1,sizeof(struct fuse)); if (f == NULL) { fprintf(stderr, "fuse: failed to allocate fuse object\n"); goto out_delete_context_key; @@ -4457,11 +4386,11 @@ struct fuse *fuse_new_common(struct fuse_chan *ch, struct fuse_args *args, return f; out_free_root: - free(root); + xmem_free(root); out_free_id_table: - free(f->id_table.array); + xmem_free(f->id_table.array); out_free_name_table: - free(f->name_table.array); + xmem_free(f->name_table.array); out_free_session: fuse_session_destroy(f->se); out_free_fs: @@ -4470,7 +4399,7 @@ struct fuse *fuse_new_common(struct fuse_chan *ch, struct fuse_args *args, fs->op.destroy = NULL; fuse_fs_destroy(f->fs); out_free: - free(f); + xmem_free(f); out_delete_context_key: fuse_delete_context_key(); out: @@ -4520,11 +4449,11 @@ void fuse_destroy(struct fuse *f) assert(list_empty(&f->partial_slabs)); assert(list_empty(&f->full_slabs)); - free(f->id_table.array); - free(f->name_table.array); + xmem_free(f->id_table.array); + xmem_free(f->name_table.array); pthread_mutex_destroy(&f->lock); fuse_session_destroy(f->se); - free(f); + xmem_free(f); fuse_delete_context_key(); } diff --git a/libfuse/lib/fuse_dirents.c b/libfuse/lib/fuse_dirents.c index 3f265428..1bd2e928 100644 --- a/libfuse/lib/fuse_dirents.c +++ b/libfuse/lib/fuse_dirents.c @@ -7,6 +7,7 @@ #include "fuse_entry.h" #include "linux_dirent64.h" #include "stat_utils.h" +#include "xmem.h" #include #include @@ -61,7 +62,7 @@ fuse_dirents_buf_resize(fuse_dirents_t *d_, if((d_->data_len + size_) >= d_->buf_len) { - p = realloc(d_->buf,(d_->buf_len * 2)); + p = xmem_realloc(d_->buf,(d_->buf_len * 2)); if(p == NULL) return -errno; @@ -381,7 +382,7 @@ fuse_dirents_init(fuse_dirents_t *d_) { void *buf; - buf = calloc(DEFAULT_SIZE,1); + buf = xmem_calloc(DEFAULT_SIZE,1); if(buf == NULL) return -ENOMEM; @@ -406,5 +407,5 @@ fuse_dirents_free(fuse_dirents_t *d_) kv_destroy(d_->offs); - free(d_->buf); + xmem_free(d_->buf); } diff --git a/libfuse/lib/fuse_loop_mt.c b/libfuse/lib/fuse_loop_mt.c index d9b76565..95c04ad5 100644 --- a/libfuse/lib/fuse_loop_mt.c +++ b/libfuse/lib/fuse_loop_mt.c @@ -10,6 +10,7 @@ #include "fuse_misc.h" #include "fuse_kernel.h" #include "fuse_i.h" +#include "xmem.h" #include #include @@ -134,28 +135,28 @@ int fuse_start_thread(pthread_t *thread_id, void *(*func)(void *), void *arg) static int fuse_loop_start_thread(struct fuse_mt *mt) { int res; - struct fuse_worker *w = malloc(sizeof(struct fuse_worker)); + struct fuse_worker *w = xmem_malloc(sizeof(struct fuse_worker)); if (!w) { fprintf(stderr, "fuse: failed to allocate worker structure\n"); return -1; } memset(w, 0, sizeof(struct fuse_worker)); w->bufsize = fuse_chan_bufsize(mt->prevch); - w->buf = calloc(w->bufsize,1); + w->buf = xmem_calloc(w->bufsize,1); w->mt = mt; if (!w->buf) { fprintf(stderr, "fuse: failed to allocate read buffer\n"); - free(w); + xmem_free(w); return -1; } res = fuse_start_thread(&w->thread_id, fuse_do_work, w); if (res == -1) { - free(w->buf); - free(w); + xmem_free(w->buf); + xmem_free(w); return -1; } - list_add_worker(w, &mt->main); + list_add_worker(w,&mt->main); return 0; } @@ -164,8 +165,8 @@ static void fuse_join_worker(struct fuse_worker *w) { pthread_join(w->thread_id, NULL); list_del_worker(w); - free(w->buf); - free(w); + xmem_free(w->buf); + xmem_free(w); } static int number_of_threads(void) diff --git a/libfuse/lib/fuse_lowlevel.c b/libfuse/lib/fuse_lowlevel.c index 8a922477..b34e7da8 100644 --- a/libfuse/lib/fuse_lowlevel.c +++ b/libfuse/lib/fuse_lowlevel.c @@ -13,6 +13,7 @@ #include "fuse_kernel.h" #include "fuse_opt.h" #include "fuse_misc.h" +#include "xmem.h" #include #include @@ -127,7 +128,7 @@ static void list_add_req(struct fuse_req *req, struct fuse_req *next) static void destroy_req(fuse_req_t req) { pthread_mutex_destroy(&req->lock); - free(req); + xmem_free(req); } void fuse_free_req(fuse_req_t req) @@ -149,7 +150,7 @@ static struct fuse_req *fuse_ll_alloc_req(struct fuse_ll *f) { struct fuse_req *req; - req = (struct fuse_req *) calloc(1, sizeof(struct fuse_req)); + req = (struct fuse_req *)xmem_calloc(1, sizeof(struct fuse_req)); if (req == NULL) { fprintf(stderr, "fuse: failed to allocate request\n"); } else { @@ -162,7 +163,6 @@ static struct fuse_req *fuse_ll_alloc_req(struct fuse_ll *f) return req; } - static int fuse_send_msg(struct fuse_ll *f, struct fuse_chan *ch, struct iovec *iov, int count) { @@ -235,7 +235,7 @@ int fuse_reply_iov(fuse_req_t req, const struct iovec *iov, int count) int res; struct iovec *padded_iov; - padded_iov = malloc((count + 1) * sizeof(struct iovec)); + padded_iov = xmem_malloc((count + 1) * sizeof(struct iovec)); if (padded_iov == NULL) return fuse_reply_err(req, ENOMEM); @@ -243,7 +243,7 @@ int fuse_reply_iov(fuse_req_t req, const struct iovec *iov, int count) count++; res = send_reply_iov(req, 0, padded_iov, count); - free(padded_iov); + xmem_free(padded_iov); return res; } @@ -451,7 +451,7 @@ static int fuse_send_data_iov_fallback(struct fuse_ll *f, struct fuse_chan *ch, mem_buf.buf[0].mem = mbuf; res = fuse_buf_copy(&mem_buf, buf, 0); if (res < 0) { - free(mbuf); + xmem_free(mbuf); return -res; } len = res; @@ -460,7 +460,7 @@ static int fuse_send_data_iov_fallback(struct fuse_ll *f, struct fuse_chan *ch, iov[iov_count].iov_len = len; iov_count++; res = fuse_send_msg(f, ch, iov, iov_count); - free(mbuf); + xmem_free(mbuf); return res; } @@ -475,7 +475,7 @@ static void fuse_ll_pipe_free(struct fuse_ll_pipe *llp) { close(llp->pipe[0]); close(llp->pipe[1]); - free(llp); + xmem_free(llp); } #ifdef HAVE_SPLICE @@ -485,13 +485,13 @@ static struct fuse_ll_pipe *fuse_ll_get_pipe(struct fuse_ll *f) if (llp == NULL) { int res; - llp = malloc(sizeof(struct fuse_ll_pipe)); + llp = xmem_malloc(sizeof(struct fuse_ll_pipe)); if (llp == NULL) return NULL; res = pipe(llp->pipe); if (res == -1) { - free(llp); + xmem_free(llp); return NULL; } @@ -499,7 +499,7 @@ static struct fuse_ll_pipe *fuse_ll_get_pipe(struct fuse_ll *f) fcntl(llp->pipe[1], F_SETFL, O_NONBLOCK) == -1) { close(llp->pipe[0]); close(llp->pipe[1]); - free(llp); + xmem_free(llp); return NULL; } @@ -674,21 +674,21 @@ static int fuse_send_data_iov(struct fuse_ll *f, struct fuse_chan *ch, * back the data from the pipe and then fall * back to regular write. */ - tmpbuf = malloc(headerlen); + tmpbuf = xmem_malloc(headerlen); if (tmpbuf == NULL) { - free(mbuf); + xmem_free(mbuf); res = ENOMEM; goto clear_pipe; } res = read_back(llp->pipe[0], tmpbuf, headerlen); - free(tmpbuf); + xmem_free(tmpbuf); if (res != 0) { - free(mbuf); + xmem_free(mbuf); goto clear_pipe; } res = read_back(llp->pipe[0], mbuf, now_len); if (res != 0) { - free(mbuf); + xmem_free(mbuf); goto clear_pipe; } len = now_len + extra_len; @@ -696,10 +696,10 @@ static int fuse_send_data_iov(struct fuse_ll *f, struct fuse_chan *ch, iov[iov_count].iov_len = len; iov_count++; res = fuse_send_msg(f, ch, iov, iov_count); - free(mbuf); + xmem_free(mbuf); return res; } - free(mbuf); + xmem_free(mbuf); res = now_len; } len = res; @@ -827,7 +827,7 @@ static struct fuse_ioctl_iovec *fuse_ioctl_iovec_copy(const struct iovec *iov, struct fuse_ioctl_iovec *fiov; size_t i; - fiov = malloc(sizeof(fiov[0]) * count); + fiov = xmem_malloc(sizeof(fiov[0]) * count); if (!fiov) return NULL; @@ -899,8 +899,8 @@ int fuse_reply_ioctl_retry(fuse_req_t req, res = send_reply_iov(req, 0, iov, count); out: - free(in_fiov); - free(out_fiov); + xmem_free(in_fiov); + xmem_free(out_fiov); return res; @@ -942,7 +942,7 @@ int fuse_reply_ioctl_iov(fuse_req_t req, int result, const struct iovec *iov, struct fuse_ioctl_out arg; int res; - padded_iov = malloc((count + 2) * sizeof(struct iovec)); + padded_iov = xmem_malloc((count + 2) * sizeof(struct iovec)); if (padded_iov == NULL) return fuse_reply_err(req, ENOMEM); @@ -954,7 +954,7 @@ int fuse_reply_ioctl_iov(fuse_req_t req, int result, const struct iovec *iov, memcpy(&padded_iov[2], iov, count * sizeof(struct iovec)); res = send_reply_iov(req, 0, padded_iov, count + 2); - free(padded_iov); + xmem_free(padded_iov); return res; } @@ -1648,7 +1648,7 @@ static struct fuse_req *check_interrupt(struct fuse_ll *f, struct fuse_req *req) if (curr->u.i.unique == req->unique) { req->interrupted = 1; list_del_req(curr); - free(curr); + xmem_free(curr); return NULL; } } @@ -1702,7 +1702,7 @@ static void do_ioctl(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) void fuse_pollhandle_destroy(struct fuse_pollhandle *ph) { - free(ph); + xmem_free(ph); } static void do_poll(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) @@ -1717,7 +1717,7 @@ static void do_poll(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) struct fuse_pollhandle *ph = NULL; if (arg->flags & FUSE_POLL_SCHEDULE_NOTIFY) { - ph = malloc(sizeof(struct fuse_pollhandle)); + ph = xmem_malloc(sizeof(struct fuse_pollhandle)); if (ph == NULL) { fuse_reply_err(req, ENOMEM); return; @@ -2224,7 +2224,7 @@ static void fuse_ll_retrieve_reply(struct fuse_notify_req *nreq, fuse_reply_none(req); } out: - free(rreq); + xmem_free(rreq); if ((ibuf->flags & FUSE_BUF_IS_FD) && bufv.idx < bufv.count) fuse_ll_clear_pipe(f); } @@ -2248,7 +2248,7 @@ int fuse_lowlevel_notify_retrieve(struct fuse_chan *ch, fuse_ino_t ino, if (f->conn.proto_minor < 15) return -ENOSYS; - rreq = malloc(sizeof(*rreq)); + rreq = xmem_malloc(sizeof(*rreq)); if (rreq == NULL) return -ENOMEM; @@ -2272,7 +2272,7 @@ int fuse_lowlevel_notify_retrieve(struct fuse_chan *ch, fuse_ino_t ino, pthread_mutex_lock(&f->lock); list_del_nreq(&rreq->nreq); pthread_mutex_unlock(&f->lock); - free(rreq); + xmem_free(rreq); } return err; @@ -2406,7 +2406,7 @@ static void fuse_ll_process_buf(void *data, const struct fuse_buf *buf, if (buf->size < tmpbuf.buf[0].size) tmpbuf.buf[0].size = buf->size; - mbuf = malloc(tmpbuf.buf[0].size); + mbuf = xmem_malloc(tmpbuf.buf[0].size); if (mbuf == NULL) { fprintf(stderr, "fuse: failed to allocate header\n"); goto clear_pipe; @@ -2498,7 +2498,7 @@ static void fuse_ll_process_buf(void *data, const struct fuse_buf *buf, void *newmbuf; err = ENOMEM; - newmbuf = realloc(mbuf, buf->size); + newmbuf = xmem_realloc(mbuf, buf->size); if (newmbuf == NULL) goto reply_err; mbuf = newmbuf; @@ -2523,7 +2523,7 @@ static void fuse_ll_process_buf(void *data, const struct fuse_buf *buf, fuse_ll_ops[in->opcode].func(req, in->nodeid, inarg); out_free: - free(mbuf); + xmem_free(mbuf); return; reply_err: @@ -2637,7 +2637,7 @@ static void fuse_ll_destroy(void *data) fuse_ll_pipe_free(llp); pthread_key_delete(f->pipe_key); pthread_mutex_destroy(&f->lock); - free(f); + xmem_free(f); } static void fuse_ll_pipe_destructor(void *data) @@ -2783,7 +2783,7 @@ struct fuse_session *fuse_lowlevel_new_common(struct fuse_args *args, op_size = sizeof(struct fuse_lowlevel_ops); } - f = (struct fuse_ll *) calloc(1, sizeof(struct fuse_ll)); + f = (struct fuse_ll *)xmem_calloc(1, sizeof(struct fuse_ll)); if (f == NULL) { fprintf(stderr, "fuse: failed to allocate fuse object\n"); goto out; @@ -2827,7 +2827,7 @@ struct fuse_session *fuse_lowlevel_new_common(struct fuse_args *args, pthread_key_delete(f->pipe_key); out_free: pthread_mutex_destroy(&f->lock); - free(f); + xmem_free(f); out: return NULL; } diff --git a/libfuse/lib/fuse_opt.c b/libfuse/lib/fuse_opt.c index 391a0666..f8edc390 100644 --- a/libfuse/lib/fuse_opt.c +++ b/libfuse/lib/fuse_opt.c @@ -8,6 +8,7 @@ #include "fuse_opt.h" #include "fuse_misc.h" +#include "xmem.h" #include #include @@ -34,8 +35,8 @@ fuse_opt_free_args(struct fuse_args *args) if (args->argv && args->allocated) { int i; for (i = 0; i < args->argc; i++) - free(args->argv[i]); - free(args->argv); + xmem_free(args->argv[i]); + xmem_free(args->argv); } args->argc = 0; args->argv = NULL; @@ -63,9 +64,9 @@ fuse_opt_add_arg(struct fuse_args *args, const char *arg) if (!newarg) return alloc_failed(); - newargv = realloc(args->argv, (args->argc + 2) * sizeof(char *)); + newargv = xmem_realloc(args->argv, (args->argc + 2) * sizeof(char *)); if (!newargv) { - free(newarg); + xmem_free(newarg); return alloc_failed(); } @@ -115,7 +116,7 @@ static int add_arg(struct fuse_opt_context *ctx, const char *arg) static int add_opt_common(char **opts, const char *opt, int esc) { unsigned oldlen = *opts ? strlen(*opts) : 0; - char *d = realloc(*opts, oldlen + 1 + strlen(opt) * 2 + 1); + char *d = xmem_realloc(*opts, oldlen + 1 + strlen(opt) * 2 + 1); if (!d) return alloc_failed(); @@ -257,14 +258,14 @@ static int process_opt_sep_arg(struct fuse_opt_context *ctx, return -1; param = ctx->argv[ctx->argctr]; - newarg = malloc(sep + strlen(param) + 1); + newarg = xmem_malloc(sep + strlen(param) + 1); if (!newarg) return alloc_failed(); memcpy(newarg, arg, sep); strcpy(newarg + sep, param); res = process_opt(ctx, opt, sep, newarg, iso); - free(newarg); + xmem_free(newarg); return res; } @@ -339,7 +340,7 @@ static int process_option_group(struct fuse_opt_context *ctx, const char *opts) return -1; } res = process_real_option_group(ctx, copy); - free(copy); + xmem_free(copy); return res; } @@ -386,7 +387,7 @@ static int opt_parse(struct fuse_opt_context *ctx) /* If option separator ("--") is the last argument, remove it */ if (ctx->nonopt && ctx->nonopt == ctx->outargs.argc && strcmp(ctx->outargs.argv[ctx->outargs.argc - 1], "--") == 0) { - free(ctx->outargs.argv[ctx->outargs.argc - 1]); + xmem_free(ctx->outargs.argv[ctx->outargs.argc - 1]); ctx->outargs.argv[--ctx->outargs.argc] = NULL; } @@ -415,7 +416,7 @@ int fuse_opt_parse(struct fuse_args *args, void *data, *args = ctx.outargs; ctx.outargs = tmp; } - free(ctx.opts); + xmem_free(ctx.opts); fuse_opt_free_args(&ctx.outargs); return res; } diff --git a/libfuse/lib/fuse_session.c b/libfuse/lib/fuse_session.c index ea6eacc5..ec3d03f4 100644 --- a/libfuse/lib/fuse_session.c +++ b/libfuse/lib/fuse_session.c @@ -8,6 +8,7 @@ #include "fuse_i.h" #include "fuse_misc.h" +#include "xmem.h" #include #include @@ -26,7 +27,7 @@ struct fuse_chan struct fuse_session *fuse_session_new(struct fuse_session_ops *op, void *data) { - struct fuse_session *se = (struct fuse_session *) malloc(sizeof(*se)); + struct fuse_session *se = (struct fuse_session *)xmem_malloc(sizeof(*se)); if (se == NULL) { fprintf(stderr, "fuse: failed to allocate session\n"); return NULL; @@ -107,7 +108,7 @@ void fuse_session_destroy(struct fuse_session *se) se->op.destroy(se->data); if (se->ch != NULL) fuse_chan_destroy(se->ch); - free(se); + xmem_free(se); } void fuse_session_exit(struct fuse_session *se) @@ -140,7 +141,7 @@ void *fuse_session_data(struct fuse_session *se) static struct fuse_chan *fuse_chan_new_common(struct fuse_chan_ops *op, int fd, size_t bufsize, void *data) { - struct fuse_chan *ch = (struct fuse_chan *) malloc(sizeof(*ch)); + struct fuse_chan *ch = (struct fuse_chan *)xmem_malloc(sizeof(*ch)); if (ch == NULL) { fprintf(stderr, "fuse: failed to allocate channel\n"); return NULL; @@ -213,5 +214,5 @@ void fuse_chan_destroy(struct fuse_chan *ch) fuse_session_remove_chan(ch); if (ch->op.destroy) ch->op.destroy(ch); - free(ch); + xmem_free(ch); } diff --git a/libfuse/lib/xmem.c b/libfuse/lib/xmem.c new file mode 100644 index 00000000..97dc09a1 --- /dev/null +++ b/libfuse/lib/xmem.c @@ -0,0 +1,323 @@ +/* + ISC License + + Copyright (c) 2020, Antonio SJ Musumeci + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#include "khash.h" + +#include + +#include +#include +#include +#include +#include +#include + +typedef struct meminfo_s meminfo_t; +struct meminfo_s +{ + const char *file; + int line; + size_t size; +}; + +#define kh_voidp_hash_func(KEY) ((((uint64_t)KEY)>>33)^((uint64_t)KEY)^(((uint64_t)KEY)<<11)) +#define kh_voidp_hash_equal(A,B) ((A) == (B)) +#define kh_meminfo_hash_func(KEY) ((((uint64_t)KEY.file)>>33)^((uint64_t)KEY.file)^(((uint64_t)KEY.file)<<11)) +#define kh_meminfo_hash_equal(A,B) (((A).file == (B).file) && ((A).line == (B).line)) + +KHASH_INIT(mem,void*,meminfo_t,1,kh_voidp_hash_func,kh_voidp_hash_equal) +KHASH_INIT(memrev,meminfo_t,uint64_t,1,kh_meminfo_hash_func,kh_meminfo_hash_equal) + + +static int g_ENABLED = 0; +static kh_mem_t *g_HASH = NULL; +static pthread_mutex_t g_HASH_MUTEX; +static int g_PRINT = 0; + +__attribute__((constructor)) +static +void +xmem_init(void) +{ + pthread_mutex_init(&g_HASH_MUTEX,NULL); + g_HASH = kh_init_mem(); +} + +__attribute__((destructor)) +static +void +xmem_destroy(void) +{ + kh_destroy_mem(g_HASH); +} + +void +xmem_enable(void) +{ + g_ENABLED = 1; +} + +void +xmem_disable(void) +{ + g_ENABLED = 0; +} + +void +xmem_print(FILE *file_) +{ + khint_t k; + kh_memrev_t *rev; + + if(file_ == NULL) + file_ = stderr; + + rev = kh_init_memrev(); + + pthread_mutex_lock(&g_HASH_MUTEX); + + for(k = kh_begin(g_HASH); k != kh_end(g_HASH); ++k) + { + if(kh_exist(g_HASH,k)) + { + int ret; + khint_t kr; + meminfo_t *mi; + + mi = &kh_value(g_HASH,k); + + kr = kh_put(memrev,rev,*mi,&ret); + if(ret == 1) + kh_value(rev,kr) = 0; + kh_value(rev,kr) += mi->size; + } + } + + pthread_mutex_unlock(&g_HASH_MUTEX); + + for(k = kh_begin(rev); k != kh_end(rev); ++k) + { + if(!kh_exist(rev,k)) + continue; + + fprintf(file_, + "%s:%d: %lu\n", + kh_key(rev,k).file, + kh_key(rev,k).line, + kh_value(rev,k)); + } + + kh_destroy_memrev(rev); +} + +void +xmem_print_to_tmp(void) +{ + FILE *file; + char filepath[256]; + + sprintf(filepath,"/tmp/xmem.%d.%ld",getpid(),time(NULL)); + file = fopen(filepath,"w+"); + + xmem_print(file); + + fclose(file); +} + +void* +xmem_malloc_(size_t size_, + const char *file_, + const int line_) +{ + void *rv; + + if(g_ENABLED == 0) + return malloc(size_); + + pthread_mutex_lock(&g_HASH_MUTEX); + + rv = malloc(size_); + if(rv != NULL) + { + int ret; + khint_t k; + meminfo_t *meminfo; + + k = kh_put(mem,g_HASH,rv,&ret); + + meminfo = &kh_value(g_HASH,k); + + meminfo->file = file_; + meminfo->line = line_; + meminfo->size = size_; + } + + pthread_mutex_unlock(&g_HASH_MUTEX); + + if(g_PRINT) + fprintf(stderr,"malloc: %lu bytes @ %p - %s:%d\n",size_,rv,file_,line_); + + return rv; +} + +void* +xmem_realloc_(void *ptr_, + size_t size_, + const char *file_, + const int line_) +{ + void *rv; + + if(g_ENABLED == 0) + return realloc(ptr_,size_); + + if(ptr_ == NULL) + return xmem_malloc_(size_,file_,line_); + + pthread_mutex_lock(&g_HASH_MUTEX); + + rv = realloc(ptr_,size_); + if(rv != NULL) + { + int ret; + khint_t k; + meminfo_t *meminfo; + + k = kh_get(mem,g_HASH,ptr_); + if(k != kh_end(g_HASH)) + { + if(rv != ptr_) + { + kh_del(mem,g_HASH,k); + k = kh_put(mem,g_HASH,rv,&ret); + } + + meminfo = &kh_value(g_HASH,k); + + meminfo->file = file_; + meminfo->line = line_; + meminfo->size = size_; + } + } + + pthread_mutex_unlock(&g_HASH_MUTEX); + + if(g_PRINT) + fprintf(stderr,"realloc: %lu bytes @ %p - %s:%d\n",size_,rv,file_,line_); + + return rv; +} + +void* +xmem_calloc_(size_t nmemb_, + size_t size_, + const char *file_, + const int line_) +{ + void *rv; + + if(g_ENABLED == 0) + return calloc(nmemb_,size_); + + pthread_mutex_lock(&g_HASH_MUTEX); + + rv = calloc(nmemb_,size_); + if(rv != NULL) + { + int ret; + khint_t k; + meminfo_t *meminfo; + + k = kh_put(mem,g_HASH,rv,&ret); + + meminfo = &kh_value(g_HASH,k); + + meminfo->file = file_; + meminfo->line = line_; + meminfo->size = (nmemb_ * size_); + } + + pthread_mutex_unlock(&g_HASH_MUTEX); + + if(g_PRINT) + fprintf(stderr,"calloc: %lu bytes @ %p - %s:%d\n",nmemb_*size_,rv,file_,line_); + + return rv; +} + +char* +xmem_strdup_(const char *s_, + const char *file_, + const int line_) +{ + char *rv; + size_t size; + + if(g_ENABLED == 0) + return strdup(s_); + + pthread_mutex_lock(&g_HASH_MUTEX); + + size = 0; + rv = strdup(s_); + if(rv != NULL) + { + int ret; + khint_t k; + meminfo_t *meminfo; + + k = kh_put(mem,g_HASH,rv,&ret); + + meminfo = &kh_value(g_HASH,k); + + size = strlen(s_); + meminfo->file = file_; + meminfo->line = line_; + meminfo->size = size; + } + + pthread_mutex_unlock(&g_HASH_MUTEX); + + if(g_PRINT) + fprintf(stderr,"strdup: %lu bytes @ %p - %s:%d\n",size,rv,file_,line_); + + return rv; +} + +void +xmem_free_(void *ptr_, + const char *file_, + const int line_) +{ + khint_t key; + + if(g_ENABLED == 0) + return free(ptr_); + + pthread_mutex_lock(&g_HASH_MUTEX); + + free(ptr_); + key = kh_get(mem,g_HASH,ptr_); + if(key != kh_end(g_HASH)) + kh_del(mem,g_HASH,key); + + pthread_mutex_unlock(&g_HASH_MUTEX); + + if(g_PRINT) + fprintf(stderr,"free: %p - %s:%d\n",ptr_,file_,line_); +} diff --git a/libfuse/lib/xmem.h b/libfuse/lib/xmem.h new file mode 100644 index 00000000..86482423 --- /dev/null +++ b/libfuse/lib/xmem.h @@ -0,0 +1,39 @@ +/* + ISC License + + Copyright (c) 2020, Antonio SJ Musumeci + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#pragma once + +#include +#include + +extern void xmem_enable(void); +extern void xmem_disable(void); + +extern void *xmem_malloc_(size_t size, const char *file, const int line); +extern void *xmem_realloc_(void *ptr, size_t size, const char *file, const int line); +extern void *xmem_calloc_(size_t nmemb, size_t size, const char *file, const int line); +extern void xmem_free_(void *ptr, const char *file, const int line); +extern char *xmem_strdup_(const char *s, const char *file, const int line); + +extern void xmem_print(FILE *file); + +#define xmem_malloc(SIZE) (xmem_malloc_((SIZE),__FILE__,__LINE__)) +#define xmem_realloc(PTR,SIZE) (xmem_realloc_((PTR),(SIZE),__FILE__,__LINE__)) +#define xmem_calloc(NMEMB,SIZE) (xmem_calloc_((NMEMB),(SIZE),__FILE__,__LINE__)) +#define xmem_free(PTR) (xmem_free_((PTR),__FILE__,__LINE__)) +#define xmem_strdup(STR) (xmem_strdup_((STR),__FILE__,__LINE__)) diff --git a/src/fuse_ioctl.cpp b/src/fuse_ioctl.cpp index de7cb971..22c50ad0 100644 --- a/src/fuse_ioctl.cpp +++ b/src/fuse_ioctl.cpp @@ -27,6 +27,8 @@ #include "str.hpp" #include "ugid.hpp" +#include "xmem.h" + #include #include @@ -44,11 +46,14 @@ typedef char IOCTL_BUF[4096]; //#define IOCTL_READ_VAL 0xD000DF01 //#define IOCTL_WRITE_VAL 0x5000DF02 //#define IOCTL_FILE_INFO 0xD000DF03 -#define IOCTL_READ_KEYS _IOWR(IOCTL_APP_TYPE,0,IOCTL_BUF) -#define IOCTL_READ_VAL _IOWR(IOCTL_APP_TYPE,1,IOCTL_BUF) -#define IOCTL_WRITE_VAL _IOW(IOCTL_APP_TYPE,2,IOCTL_BUF) -#define IOCTL_FILE_INFO _IOWR(IOCTL_APP_TYPE,3,IOCTL_BUF) - +#define IOCTL_READ_KEYS _IOWR(IOCTL_APP_TYPE,0,IOCTL_BUF) +#define IOCTL_READ_VAL _IOWR(IOCTL_APP_TYPE,1,IOCTL_BUF) +#define IOCTL_WRITE_VAL _IOW(IOCTL_APP_TYPE,2,IOCTL_BUF) +#define IOCTL_FILE_INFO _IOWR(IOCTL_APP_TYPE,3,IOCTL_BUF) +#define IOCTL_XMEM_PRINT _IO(IOCTL_APP_TYPE,4) +#define IOCTL_XMEM_ENABLE _IO(IOCTL_APP_TYPE,5) +#define IOCTL_XMEM_DISABLE _IO(IOCTL_APP_TYPE,6) +#define IOCTL_PRINT_NODES _IO(IOCTL_APP_TYPE,7) #ifndef FS_IOC_GETFLAGS # define FS_IOC_GETFLAGS _IOR('f',1,long) @@ -364,6 +369,38 @@ namespace l return -ENOATTR; } + + static + int + ioctl_type_DF(unsigned long cmd_, + fuse_file_info *ffi_, + void *data_) + { + switch(cmd_) + { + case IOCTL_READ_KEYS: + return l::read_keys(data_); + case IOCTL_READ_VAL: + return l::read_val(data_); + case IOCTL_WRITE_VAL: + return l::write_val(data_); + case IOCTL_FILE_INFO: + return l::file_info(ffi_,data_); + case IOCTL_XMEM_PRINT: + xmem_print_to_tmp(); + return 0; + case IOCTL_XMEM_ENABLE: + xmem_enable(); + return 0; + case IOCTL_XMEM_DISABLE: + xmem_disable(); + return 0; + case IOCTL_PRINT_NODES: + return -0xDEADBEEF; + } + + return 0; + } } namespace FUSE @@ -376,17 +413,8 @@ namespace FUSE void *data_, uint32_t *out_bufsz_) { - switch(cmd_) - { - case IOCTL_READ_KEYS: - return l::read_keys(data_); - case IOCTL_READ_VAL: - return l::read_val(data_); - case IOCTL_WRITE_VAL: - return l::write_val(data_); - case IOCTL_FILE_INFO: - return l::file_info(ffi_,data_); - } + if(((cmd_ >> 8) & 0xFF) == IOCTL_APP_TYPE) + return l::ioctl_type_DF(cmd_,ffi_,data_); if(flags_ & FUSE_IOCTL_DIR) return l::ioctl_dir(ffi_,cmd_,data_,out_bufsz_);