^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: GPL-2.0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2012 Red Hat. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #ifndef BTRFS_RCU_STRING_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #define BTRFS_RCU_STRING_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) struct rcu_string {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) struct rcu_head rcu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) char str[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) static inline struct rcu_string *rcu_string_strdup(const char *src, gfp_t mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) size_t len = strlen(src) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) struct rcu_string *ret = kzalloc(sizeof(struct rcu_string) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) (len * sizeof(char)), mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) strncpy(ret->str, src, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) static inline void rcu_string_free(struct rcu_string *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) if (str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) kfree_rcu(str, rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #define printk_in_rcu(fmt, ...) do { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) rcu_read_lock(); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) printk(fmt, __VA_ARGS__); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) rcu_read_unlock(); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #define printk_ratelimited_in_rcu(fmt, ...) do { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) rcu_read_lock(); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) printk_ratelimited(fmt, __VA_ARGS__); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) rcu_read_unlock(); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #define rcu_str_deref(rcu_str) ({ \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) struct rcu_string *__str = rcu_dereference(rcu_str); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) __str->str; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) })
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) #endif