Skip to content

Commit 12c9209

Browse files
Al Virogregkh
authored andcommitted
debugfs: allow to store an additional opaque pointer at file creation
Set by debugfs_create_file_aux(name, mode, parent, data, aux, fops). Plain debugfs_create_file() has it set to NULL. Accessed by debugfs_get_aux(file). Convenience macros for numeric opaque data - debugfs_create_file_aux_num and debugfs_get_aux_num, resp. Signed-off-by: Al Viro <[email protected]> Reviewed-by: Christian Brauner <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 9568880 commit 12c9209

File tree

4 files changed

+42
-6
lines changed

4 files changed

+42
-6
lines changed

fs/debugfs/file.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ const struct file_operations debugfs_noop_file_operations = {
4747

4848
#define F_DENTRY(filp) ((filp)->f_path.dentry)
4949

50+
const void *debugfs_get_aux(const struct file *file)
51+
{
52+
return DEBUGFS_I(file_inode(file))->aux;
53+
}
54+
EXPORT_SYMBOL_GPL(debugfs_get_aux);
55+
5056
const struct file_operations *debugfs_real_fops(const struct file *filp)
5157
{
5258
struct debugfs_fsdata *fsd = F_DENTRY(filp)->d_fsdata;

fs/debugfs/inode.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,7 @@ static struct dentry *end_creating(struct dentry *dentry)
424424

425425
static struct dentry *__debugfs_create_file(const char *name, umode_t mode,
426426
struct dentry *parent, void *data,
427+
const void *aux,
427428
const struct file_operations *proxy_fops,
428429
const void *real_fops)
429430
{
@@ -458,6 +459,7 @@ static struct dentry *__debugfs_create_file(const char *name, umode_t mode,
458459
proxy_fops = &debugfs_noop_file_operations;
459460
inode->i_fop = proxy_fops;
460461
DEBUGFS_I(inode)->raw = real_fops;
462+
DEBUGFS_I(inode)->aux = aux;
461463

462464
d_instantiate(dentry, inode);
463465
fsnotify_create(d_inode(dentry->d_parent), dentry);
@@ -466,19 +468,21 @@ static struct dentry *__debugfs_create_file(const char *name, umode_t mode,
466468

467469
struct dentry *debugfs_create_file_full(const char *name, umode_t mode,
468470
struct dentry *parent, void *data,
471+
const void *aux,
469472
const struct file_operations *fops)
470473
{
471-
return __debugfs_create_file(name, mode, parent, data,
474+
return __debugfs_create_file(name, mode, parent, data, aux,
472475
&debugfs_full_proxy_file_operations,
473476
fops);
474477
}
475478
EXPORT_SYMBOL_GPL(debugfs_create_file_full);
476479

477480
struct dentry *debugfs_create_file_short(const char *name, umode_t mode,
478-
struct dentry *parent, void *data,
479-
const struct debugfs_short_fops *fops)
481+
struct dentry *parent, void *data,
482+
const void *aux,
483+
const struct debugfs_short_fops *fops)
480484
{
481-
return __debugfs_create_file(name, mode, parent, data,
485+
return __debugfs_create_file(name, mode, parent, data, aux,
482486
&debugfs_full_short_proxy_file_operations,
483487
fops);
484488
}
@@ -516,7 +520,7 @@ struct dentry *debugfs_create_file_unsafe(const char *name, umode_t mode,
516520
const struct file_operations *fops)
517521
{
518522

519-
return __debugfs_create_file(name, mode, parent, data,
523+
return __debugfs_create_file(name, mode, parent, data, NULL,
520524
&debugfs_open_proxy_file_operations,
521525
fops);
522526
}

fs/debugfs/internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ struct debugfs_inode_info {
1919
const struct debugfs_short_fops *short_fops;
2020
debugfs_automount_t automount;
2121
};
22+
const void *aux;
2223
};
2324

2425
static inline struct debugfs_inode_info *DEBUGFS_I(struct inode *inode)

include/linux/debugfs.h

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,11 @@ struct debugfs_short_fops {
7979

8080
struct dentry *debugfs_create_file_full(const char *name, umode_t mode,
8181
struct dentry *parent, void *data,
82+
const void *aux,
8283
const struct file_operations *fops);
8384
struct dentry *debugfs_create_file_short(const char *name, umode_t mode,
8485
struct dentry *parent, void *data,
86+
const void *aux,
8587
const struct debugfs_short_fops *fops);
8688

8789
/**
@@ -126,7 +128,15 @@ struct dentry *debugfs_create_file_short(const char *name, umode_t mode,
126128
const struct debugfs_short_fops *: debugfs_create_file_short, \
127129
struct file_operations *: debugfs_create_file_full, \
128130
struct debugfs_short_fops *: debugfs_create_file_short) \
129-
(name, mode, parent, data, fops)
131+
(name, mode, parent, data, NULL, fops)
132+
133+
#define debugfs_create_file_aux(name, mode, parent, data, aux, fops) \
134+
_Generic(fops, \
135+
const struct file_operations *: debugfs_create_file_full, \
136+
const struct debugfs_short_fops *: debugfs_create_file_short, \
137+
struct file_operations *: debugfs_create_file_full, \
138+
struct debugfs_short_fops *: debugfs_create_file_short) \
139+
(name, mode, parent, data, aux, fops)
130140

131141
struct dentry *debugfs_create_file_unsafe(const char *name, umode_t mode,
132142
struct dentry *parent, void *data,
@@ -153,6 +163,7 @@ void debugfs_remove(struct dentry *dentry);
153163
void debugfs_lookup_and_remove(const char *name, struct dentry *parent);
154164

155165
const struct file_operations *debugfs_real_fops(const struct file *filp);
166+
const void *debugfs_get_aux(const struct file *file);
156167

157168
int debugfs_file_get(struct dentry *dentry);
158169
void debugfs_file_put(struct dentry *dentry);
@@ -259,6 +270,14 @@ static inline struct dentry *debugfs_lookup(const char *name,
259270
return ERR_PTR(-ENODEV);
260271
}
261272

273+
static inline struct dentry *debugfs_create_file_aux(const char *name,
274+
umode_t mode, struct dentry *parent,
275+
void *data, void *aux,
276+
const void *fops)
277+
{
278+
return ERR_PTR(-ENODEV);
279+
}
280+
262281
static inline struct dentry *debugfs_create_file(const char *name, umode_t mode,
263282
struct dentry *parent, void *data,
264283
const void *fops)
@@ -312,6 +331,7 @@ static inline void debugfs_lookup_and_remove(const char *name,
312331
{ }
313332

314333
const struct file_operations *debugfs_real_fops(const struct file *filp);
334+
void *debugfs_get_aux(const struct file *file);
315335

316336
static inline int debugfs_file_get(struct dentry *dentry)
317337
{
@@ -452,6 +472,11 @@ static inline ssize_t debugfs_read_file_str(struct file *file,
452472

453473
#endif
454474

475+
#define debugfs_create_file_aux_num(name, mode, parent, data, n, fops) \
476+
debugfs_create_file_aux(name, mode, parent, data, \
477+
(void *)(unsigned long)n, fops)
478+
#define debugfs_get_aux_num(f) (unsigned long)debugfs_get_aux(f)
479+
455480
/**
456481
* debugfs_create_xul - create a debugfs file that is used to read and write an
457482
* unsigned long value, formatted in hexadecimal

0 commit comments

Comments
 (0)