Commit c1feab95 authored by Al Viro's avatar Al Viro
Browse files

add a string-to-qstr constructor



Quite a few places want to build a struct qstr by given string;
it would be convenient to have a primitive doing that, rather
than open-coding it via QSTR_INIT().

The closest approximation was in bcachefs, but that expands to
initializer list - {.len = strlen(string), .name = string}.
It would be more useful to have it as compound literal -
(struct qstr){.len = strlen(string), .name = string}.

Unlike initializer list it's a valid expression.  What's more,
it's a valid lvalue - it's an equivalent of anonymous local
variable with such initializer, so the things like
	path->dentry = d_alloc_pseudo(mnt->mnt_sb, &QSTR(name));
are valid.  It can also be used as initializer, with identical
effect -
	struct qstr x = (struct qstr){.name = s, .len = strlen(s)};
is equivalent to
	struct qstr anon_variable = {.name = s, .len = strlen(s)};
	struct qstr x = anon_variable;
	// anon_variable is never used after that point
and any even remotely sane compiler will manage to collapse that
into
	struct qstr x = {.name = s, .len = strlen(s)};

What compound literals can't be used for is initialization of
global variables, but those are covered by QSTR_INIT().

This commit lifts definition(s) of QSTR() into linux/dcache.h,
converts it to compound literal (all bcachefs users are fine
with that) and converts assorted open-coded instances to using
that.

Signed-off-by: default avatarAl Viro <viro@zeniv.linux.org.uk>
parent 5f4e6f7f
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -60,14 +60,14 @@ static struct inode *anon_inode_make_secure_inode(
	const struct inode *context_inode)
{
	struct inode *inode;
	const struct qstr qname = QSTR_INIT(name, strlen(name));
	int error;

	inode = alloc_anon_inode(anon_inode_mnt->mnt_sb);
	if (IS_ERR(inode))
		return inode;
	inode->i_flags &= ~S_PRIVATE;
	error =	security_inode_init_security_anon(inode, &qname, context_inode);
	error =	security_inode_init_security_anon(inode, &QSTR(name),
						  context_inode);
	if (error) {
		iput(inode);
		return ERR_PTR(error);
+1 −1
Original line number Diff line number Diff line
@@ -405,7 +405,7 @@ static int reattach_inode(struct btree_trans *trans, struct bch_inode_unpacked *
		return ret;

	struct bch_hash_info dir_hash = bch2_hash_info_init(c, &lostfound);
	struct qstr name = (struct qstr) QSTR(name_buf);
	struct qstr name = QSTR(name_buf);

	inode->bi_dir = lostfound.bi_inum;

+0 −2
Original line number Diff line number Diff line
@@ -32,8 +32,6 @@
#include <linux/sort.h>
#include <linux/stat.h>

#define QSTR(n) { { { .len = strlen(n) } }, .name = n }

void bch2_btree_lost_data(struct bch_fs *c, enum btree_id btree)
{
	if (btree >= BTREE_ID_NR_MAX)
+0 −2
Original line number Diff line number Diff line
@@ -647,8 +647,6 @@ static inline int cmp_le32(__le32 l, __le32 r)

#include <linux/uuid.h>

#define QSTR(n) { { { .len = strlen(n) } }, .name = n }

static inline bool qstr_eq(const struct qstr l, const struct qstr r)
{
	return l.len == r.len && !memcmp(l.name, r.name, l.len);
+1 −1
Original line number Diff line number Diff line
@@ -407,7 +407,7 @@ int erofs_getxattr(struct inode *inode, int index, const char *name,
	}

	it.index = index;
	it.name = (struct qstr)QSTR_INIT(name, strlen(name));
	it.name = QSTR(name);
	if (it.name.len > EROFS_NAME_LEN)
		return -ERANGE;

Loading