xref: /linux/fs/btrfs/rcu-string.h (revision 79790b6818e96c58fe2bffee1b418c16e64e7b80)
19888c340SDavid Sterba /* SPDX-License-Identifier: GPL-2.0 */
2606686eeSJosef Bacik /*
3606686eeSJosef Bacik  * Copyright (C) 2012 Red Hat.  All rights reserved.
4606686eeSJosef Bacik  */
5606686eeSJosef Bacik 
69888c340SDavid Sterba #ifndef BTRFS_RCU_STRING_H
79888c340SDavid Sterba #define BTRFS_RCU_STRING_H
89888c340SDavid Sterba 
9*22b46bdcSDavid Sterba #include <linux/types.h>
10*22b46bdcSDavid Sterba #include <linux/string.h>
11*22b46bdcSDavid Sterba #include <linux/slab.h>
12*22b46bdcSDavid Sterba #include <linux/rcupdate.h>
13*22b46bdcSDavid Sterba #include <linux/printk.h>
14*22b46bdcSDavid Sterba 
15606686eeSJosef Bacik struct rcu_string {
16606686eeSJosef Bacik 	struct rcu_head rcu;
177593f4c5SGustavo A. R. Silva 	char str[];
18606686eeSJosef Bacik };
19606686eeSJosef Bacik 
rcu_string_strdup(const char * src,gfp_t mask)20606686eeSJosef Bacik static inline struct rcu_string *rcu_string_strdup(const char *src, gfp_t mask)
21606686eeSJosef Bacik {
22606686eeSJosef Bacik 	size_t len = strlen(src) + 1;
23606686eeSJosef Bacik 	struct rcu_string *ret = kzalloc(sizeof(struct rcu_string) +
24606686eeSJosef Bacik 					 (len * sizeof(char)), mask);
25606686eeSJosef Bacik 	if (!ret)
26606686eeSJosef Bacik 		return ret;
2763d5429fSArtem Chernyshev 	/* Warn if the source got unexpectedly truncated. */
2863d5429fSArtem Chernyshev 	if (WARN_ON(strscpy(ret->str, src, len) < 0)) {
2963d5429fSArtem Chernyshev 		kfree(ret);
3063d5429fSArtem Chernyshev 		return NULL;
3163d5429fSArtem Chernyshev 	}
32606686eeSJosef Bacik 	return ret;
33606686eeSJosef Bacik }
34606686eeSJosef Bacik 
rcu_string_free(struct rcu_string * str)35606686eeSJosef Bacik static inline void rcu_string_free(struct rcu_string *str)
36606686eeSJosef Bacik {
37606686eeSJosef Bacik 	if (str)
38606686eeSJosef Bacik 		kfree_rcu(str, rcu);
39606686eeSJosef Bacik }
40606686eeSJosef Bacik 
41606686eeSJosef Bacik #define printk_in_rcu(fmt, ...) do {	\
42606686eeSJosef Bacik 	rcu_read_lock();		\
43606686eeSJosef Bacik 	printk(fmt, __VA_ARGS__);	\
44606686eeSJosef Bacik 	rcu_read_unlock();		\
45606686eeSJosef Bacik } while (0)
46606686eeSJosef Bacik 
47606686eeSJosef Bacik #define printk_ratelimited_in_rcu(fmt, ...) do {	\
48606686eeSJosef Bacik 	rcu_read_lock();				\
49606686eeSJosef Bacik 	printk_ratelimited(fmt, __VA_ARGS__);		\
50606686eeSJosef Bacik 	rcu_read_unlock();				\
51606686eeSJosef Bacik } while (0)
52606686eeSJosef Bacik 
53606686eeSJosef Bacik #define rcu_str_deref(rcu_str) ({				\
54606686eeSJosef Bacik 	struct rcu_string *__str = rcu_dereference(rcu_str);	\
55606686eeSJosef Bacik 	__str->str;						\
56606686eeSJosef Bacik })
579888c340SDavid Sterba 
589888c340SDavid Sterba #endif
59