Skip to content

Commit c1feab9

Browse files
author
Al Viro
committed
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: Al Viro <[email protected]>
1 parent 5f4e6f7 commit c1feab9

File tree

10 files changed

+13
-23
lines changed

10 files changed

+13
-23
lines changed

fs/anon_inodes.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,14 @@ static struct inode *anon_inode_make_secure_inode(
6060
const struct inode *context_inode)
6161
{
6262
struct inode *inode;
63-
const struct qstr qname = QSTR_INIT(name, strlen(name));
6463
int error;
6564

6665
inode = alloc_anon_inode(anon_inode_mnt->mnt_sb);
6766
if (IS_ERR(inode))
6867
return inode;
6968
inode->i_flags &= ~S_PRIVATE;
70-
error = security_inode_init_security_anon(inode, &qname, context_inode);
69+
error = security_inode_init_security_anon(inode, &QSTR(name),
70+
context_inode);
7171
if (error) {
7272
iput(inode);
7373
return ERR_PTR(error);

fs/bcachefs/fsck.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ static int reattach_inode(struct btree_trans *trans, struct bch_inode_unpacked *
405405
return ret;
406406

407407
struct bch_hash_info dir_hash = bch2_hash_info_init(c, &lostfound);
408-
struct qstr name = (struct qstr) QSTR(name_buf);
408+
struct qstr name = QSTR(name_buf);
409409

410410
inode->bi_dir = lostfound.bi_inum;
411411

fs/bcachefs/recovery.c

-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@
3232
#include <linux/sort.h>
3333
#include <linux/stat.h>
3434

35-
#define QSTR(n) { { { .len = strlen(n) } }, .name = n }
36-
3735
void bch2_btree_lost_data(struct bch_fs *c, enum btree_id btree)
3836
{
3937
if (btree >= BTREE_ID_NR_MAX)

fs/bcachefs/util.h

-2
Original file line numberDiff line numberDiff line change
@@ -647,8 +647,6 @@ static inline int cmp_le32(__le32 l, __le32 r)
647647

648648
#include <linux/uuid.h>
649649

650-
#define QSTR(n) { { { .len = strlen(n) } }, .name = n }
651-
652650
static inline bool qstr_eq(const struct qstr l, const struct qstr r)
653651
{
654652
return l.len == r.len && !memcmp(l.name, r.name, l.len);

fs/erofs/xattr.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ int erofs_getxattr(struct inode *inode, int index, const char *name,
407407
}
408408

409409
it.index = index;
410-
it.name = (struct qstr)QSTR_INIT(name, strlen(name));
410+
it.name = QSTR(name);
411411
if (it.name.len > EROFS_NAME_LEN)
412412
return -ERANGE;
413413

fs/file_table.c

+1-3
Original file line numberDiff line numberDiff line change
@@ -351,9 +351,7 @@ static struct file *alloc_file(const struct path *path, int flags,
351351
static inline int alloc_path_pseudo(const char *name, struct inode *inode,
352352
struct vfsmount *mnt, struct path *path)
353353
{
354-
struct qstr this = QSTR_INIT(name, strlen(name));
355-
356-
path->dentry = d_alloc_pseudo(mnt->mnt_sb, &this);
354+
path->dentry = d_alloc_pseudo(mnt->mnt_sb, &QSTR(name));
357355
if (!path->dentry)
358356
return -ENOMEM;
359357
path->mnt = mntget(mnt);

fs/kernfs/file.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -927,7 +927,7 @@ static void kernfs_notify_workfn(struct work_struct *work)
927927
if (!inode)
928928
continue;
929929

930-
name = (struct qstr)QSTR_INIT(kn->name, strlen(kn->name));
930+
name = QSTR(kn->name);
931931
parent = kernfs_get_parent(kn);
932932
if (parent) {
933933
p_inode = ilookup(info->sb, kernfs_ino(parent));

include/linux/dcache.h

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ struct qstr {
5757
};
5858

5959
#define QSTR_INIT(n,l) { { { .len = l } }, .name = n }
60+
#define QSTR(n) (struct qstr)QSTR_INIT(n, strlen(n))
6061

6162
extern const struct qstr empty_name;
6263
extern const struct qstr slash_name;

mm/secretmem.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -195,14 +195,13 @@ static struct file *secretmem_file_create(unsigned long flags)
195195
struct file *file;
196196
struct inode *inode;
197197
const char *anon_name = "[secretmem]";
198-
const struct qstr qname = QSTR_INIT(anon_name, strlen(anon_name));
199198
int err;
200199

201200
inode = alloc_anon_inode(secretmem_mnt->mnt_sb);
202201
if (IS_ERR(inode))
203202
return ERR_CAST(inode);
204203

205-
err = security_inode_init_security_anon(inode, &qname, NULL);
204+
err = security_inode_init_security_anon(inode, &QSTR(anon_name), NULL);
206205
if (err) {
207206
file = ERR_PTR(err);
208207
goto err_free_inode;

net/sunrpc/rpc_pipe.c

+5-9
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ static int __rpc_rmpipe(struct inode *dir, struct dentry *dentry)
630630
static struct dentry *__rpc_lookup_create_exclusive(struct dentry *parent,
631631
const char *name)
632632
{
633-
struct qstr q = QSTR_INIT(name, strlen(name));
633+
struct qstr q = QSTR(name);
634634
struct dentry *dentry = d_hash_and_lookup(parent, &q);
635635
if (!dentry) {
636636
dentry = d_alloc(parent, &q);
@@ -1190,8 +1190,7 @@ static const struct rpc_filelist files[] = {
11901190
struct dentry *rpc_d_lookup_sb(const struct super_block *sb,
11911191
const unsigned char *dir_name)
11921192
{
1193-
struct qstr dir = QSTR_INIT(dir_name, strlen(dir_name));
1194-
return d_hash_and_lookup(sb->s_root, &dir);
1193+
return d_hash_and_lookup(sb->s_root, &QSTR(dir_name));
11951194
}
11961195
EXPORT_SYMBOL_GPL(rpc_d_lookup_sb);
11971196

@@ -1300,11 +1299,9 @@ rpc_gssd_dummy_populate(struct dentry *root, struct rpc_pipe *pipe_data)
13001299
struct dentry *gssd_dentry;
13011300
struct dentry *clnt_dentry = NULL;
13021301
struct dentry *pipe_dentry = NULL;
1303-
struct qstr q = QSTR_INIT(files[RPCAUTH_gssd].name,
1304-
strlen(files[RPCAUTH_gssd].name));
13051302

13061303
/* We should never get this far if "gssd" doesn't exist */
1307-
gssd_dentry = d_hash_and_lookup(root, &q);
1304+
gssd_dentry = d_hash_and_lookup(root, &QSTR(files[RPCAUTH_gssd].name));
13081305
if (!gssd_dentry)
13091306
return ERR_PTR(-ENOENT);
13101307

@@ -1314,9 +1311,8 @@ rpc_gssd_dummy_populate(struct dentry *root, struct rpc_pipe *pipe_data)
13141311
goto out;
13151312
}
13161313

1317-
q.name = gssd_dummy_clnt_dir[0].name;
1318-
q.len = strlen(gssd_dummy_clnt_dir[0].name);
1319-
clnt_dentry = d_hash_and_lookup(gssd_dentry, &q);
1314+
clnt_dentry = d_hash_and_lookup(gssd_dentry,
1315+
&QSTR(gssd_dummy_clnt_dir[0].name));
13201316
if (!clnt_dentry) {
13211317
__rpc_depopulate(gssd_dentry, gssd_dummy_clnt_dir, 0, 1);
13221318
pipe_dentry = ERR_PTR(-ENOENT);

0 commit comments

Comments
 (0)