fs/proc: Introduce list_for_each_table_entry for proc sysctl
Use the list_for_each_table_entry macro to optimize the scenario of traverse ctl_table. This make the code neater and easier to understand. Suggested-by: Davidlohr Bueso<dave@stgolabs.net> Signed-off-by: Meng Tang <tangmeng@uniontech.com> [updated the sysctl_check_table() hunk due to some changes upstream] Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>
This commit is contained in:
committed by
Luis Chamberlain
parent
3c6a4cba31
commit
cb55f27ac9
@@ -19,6 +19,9 @@
|
|||||||
#include <linux/kmemleak.h>
|
#include <linux/kmemleak.h>
|
||||||
#include "internal.h"
|
#include "internal.h"
|
||||||
|
|
||||||
|
#define list_for_each_table_entry(entry, table) \
|
||||||
|
for ((entry) = (table); (entry)->procname; (entry)++)
|
||||||
|
|
||||||
static const struct dentry_operations proc_sys_dentry_operations;
|
static const struct dentry_operations proc_sys_dentry_operations;
|
||||||
static const struct file_operations proc_sys_file_operations;
|
static const struct file_operations proc_sys_file_operations;
|
||||||
static const struct inode_operations proc_sys_inode_operations;
|
static const struct inode_operations proc_sys_inode_operations;
|
||||||
@@ -215,15 +218,19 @@ static void init_header(struct ctl_table_header *head,
|
|||||||
INIT_HLIST_HEAD(&head->inodes);
|
INIT_HLIST_HEAD(&head->inodes);
|
||||||
if (node) {
|
if (node) {
|
||||||
struct ctl_table *entry;
|
struct ctl_table *entry;
|
||||||
for (entry = table; entry->procname; entry++, node++)
|
|
||||||
|
list_for_each_table_entry(entry, table) {
|
||||||
node->header = head;
|
node->header = head;
|
||||||
|
node++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void erase_header(struct ctl_table_header *head)
|
static void erase_header(struct ctl_table_header *head)
|
||||||
{
|
{
|
||||||
struct ctl_table *entry;
|
struct ctl_table *entry;
|
||||||
for (entry = head->ctl_table; entry->procname; entry++)
|
|
||||||
|
list_for_each_table_entry(entry, head->ctl_table)
|
||||||
erase_entry(head, entry);
|
erase_entry(head, entry);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -248,7 +255,7 @@ static int insert_header(struct ctl_dir *dir, struct ctl_table_header *header)
|
|||||||
err = insert_links(header);
|
err = insert_links(header);
|
||||||
if (err)
|
if (err)
|
||||||
goto fail_links;
|
goto fail_links;
|
||||||
for (entry = header->ctl_table; entry->procname; entry++) {
|
list_for_each_table_entry(entry, header->ctl_table) {
|
||||||
err = insert_entry(header, entry);
|
err = insert_entry(header, entry);
|
||||||
if (err)
|
if (err)
|
||||||
goto fail;
|
goto fail;
|
||||||
@@ -1130,35 +1137,36 @@ static int sysctl_check_table_array(const char *path, struct ctl_table *table)
|
|||||||
|
|
||||||
static int sysctl_check_table(const char *path, struct ctl_table *table)
|
static int sysctl_check_table(const char *path, struct ctl_table *table)
|
||||||
{
|
{
|
||||||
|
struct ctl_table *entry;
|
||||||
int err = 0;
|
int err = 0;
|
||||||
for (; table->procname; table++) {
|
list_for_each_table_entry(entry, table) {
|
||||||
if (table->child)
|
if (entry->child)
|
||||||
err |= sysctl_err(path, table, "Not a file");
|
err |= sysctl_err(path, entry, "Not a file");
|
||||||
|
|
||||||
if ((table->proc_handler == proc_dostring) ||
|
if ((entry->proc_handler == proc_dostring) ||
|
||||||
(table->proc_handler == proc_dointvec) ||
|
(entry->proc_handler == proc_dointvec) ||
|
||||||
(table->proc_handler == proc_douintvec) ||
|
(entry->proc_handler == proc_douintvec) ||
|
||||||
(table->proc_handler == proc_douintvec_minmax) ||
|
(entry->proc_handler == proc_douintvec_minmax) ||
|
||||||
(table->proc_handler == proc_dointvec_minmax) ||
|
(entry->proc_handler == proc_dointvec_minmax) ||
|
||||||
(table->proc_handler == proc_dou8vec_minmax) ||
|
(entry->proc_handler == proc_dou8vec_minmax) ||
|
||||||
(table->proc_handler == proc_dointvec_jiffies) ||
|
(entry->proc_handler == proc_dointvec_jiffies) ||
|
||||||
(table->proc_handler == proc_dointvec_userhz_jiffies) ||
|
(entry->proc_handler == proc_dointvec_userhz_jiffies) ||
|
||||||
(table->proc_handler == proc_dointvec_ms_jiffies) ||
|
(entry->proc_handler == proc_dointvec_ms_jiffies) ||
|
||||||
(table->proc_handler == proc_doulongvec_minmax) ||
|
(entry->proc_handler == proc_doulongvec_minmax) ||
|
||||||
(table->proc_handler == proc_doulongvec_ms_jiffies_minmax)) {
|
(entry->proc_handler == proc_doulongvec_ms_jiffies_minmax)) {
|
||||||
if (!table->data)
|
if (!entry->data)
|
||||||
err |= sysctl_err(path, table, "No data");
|
err |= sysctl_err(path, entry, "No data");
|
||||||
if (!table->maxlen)
|
if (!entry->maxlen)
|
||||||
err |= sysctl_err(path, table, "No maxlen");
|
err |= sysctl_err(path, entry, "No maxlen");
|
||||||
else
|
else
|
||||||
err |= sysctl_check_table_array(path, table);
|
err |= sysctl_check_table_array(path, entry);
|
||||||
}
|
}
|
||||||
if (!table->proc_handler)
|
if (!entry->proc_handler)
|
||||||
err |= sysctl_err(path, table, "No proc_handler");
|
err |= sysctl_err(path, entry, "No proc_handler");
|
||||||
|
|
||||||
if ((table->mode & (S_IRUGO|S_IWUGO)) != table->mode)
|
if ((entry->mode & (S_IRUGO|S_IWUGO)) != entry->mode)
|
||||||
err |= sysctl_err(path, table, "bogus .mode 0%o",
|
err |= sysctl_err(path, entry, "bogus .mode 0%o",
|
||||||
table->mode);
|
entry->mode);
|
||||||
}
|
}
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
@@ -1174,7 +1182,7 @@ static struct ctl_table_header *new_links(struct ctl_dir *dir, struct ctl_table
|
|||||||
|
|
||||||
name_bytes = 0;
|
name_bytes = 0;
|
||||||
nr_entries = 0;
|
nr_entries = 0;
|
||||||
for (entry = table; entry->procname; entry++) {
|
list_for_each_table_entry(entry, table) {
|
||||||
nr_entries++;
|
nr_entries++;
|
||||||
name_bytes += strlen(entry->procname) + 1;
|
name_bytes += strlen(entry->procname) + 1;
|
||||||
}
|
}
|
||||||
@@ -1191,14 +1199,16 @@ static struct ctl_table_header *new_links(struct ctl_dir *dir, struct ctl_table
|
|||||||
node = (struct ctl_node *)(links + 1);
|
node = (struct ctl_node *)(links + 1);
|
||||||
link_table = (struct ctl_table *)(node + nr_entries);
|
link_table = (struct ctl_table *)(node + nr_entries);
|
||||||
link_name = (char *)&link_table[nr_entries + 1];
|
link_name = (char *)&link_table[nr_entries + 1];
|
||||||
|
link = link_table;
|
||||||
|
|
||||||
for (link = link_table, entry = table; entry->procname; link++, entry++) {
|
list_for_each_table_entry(entry, table) {
|
||||||
int len = strlen(entry->procname) + 1;
|
int len = strlen(entry->procname) + 1;
|
||||||
memcpy(link_name, entry->procname, len);
|
memcpy(link_name, entry->procname, len);
|
||||||
link->procname = link_name;
|
link->procname = link_name;
|
||||||
link->mode = S_IFLNK|S_IRWXUGO;
|
link->mode = S_IFLNK|S_IRWXUGO;
|
||||||
link->data = link_root;
|
link->data = link_root;
|
||||||
link_name += len;
|
link_name += len;
|
||||||
|
link++;
|
||||||
}
|
}
|
||||||
init_header(links, dir->header.root, dir->header.set, node, link_table);
|
init_header(links, dir->header.root, dir->header.set, node, link_table);
|
||||||
links->nreg = nr_entries;
|
links->nreg = nr_entries;
|
||||||
@@ -1213,7 +1223,7 @@ static bool get_links(struct ctl_dir *dir,
|
|||||||
struct ctl_table *entry, *link;
|
struct ctl_table *entry, *link;
|
||||||
|
|
||||||
/* Are there links available for every entry in table? */
|
/* Are there links available for every entry in table? */
|
||||||
for (entry = table; entry->procname; entry++) {
|
list_for_each_table_entry(entry, table) {
|
||||||
const char *procname = entry->procname;
|
const char *procname = entry->procname;
|
||||||
link = find_entry(&head, dir, procname, strlen(procname));
|
link = find_entry(&head, dir, procname, strlen(procname));
|
||||||
if (!link)
|
if (!link)
|
||||||
@@ -1226,7 +1236,7 @@ static bool get_links(struct ctl_dir *dir,
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* The checks passed. Increase the registration count on the links */
|
/* The checks passed. Increase the registration count on the links */
|
||||||
for (entry = table; entry->procname; entry++) {
|
list_for_each_table_entry(entry, table) {
|
||||||
const char *procname = entry->procname;
|
const char *procname = entry->procname;
|
||||||
link = find_entry(&head, dir, procname, strlen(procname));
|
link = find_entry(&head, dir, procname, strlen(procname));
|
||||||
head->nreg++;
|
head->nreg++;
|
||||||
@@ -1329,7 +1339,7 @@ struct ctl_table_header *__register_sysctl_table(
|
|||||||
struct ctl_node *node;
|
struct ctl_node *node;
|
||||||
int nr_entries = 0;
|
int nr_entries = 0;
|
||||||
|
|
||||||
for (entry = table; entry->procname; entry++)
|
list_for_each_table_entry(entry, table)
|
||||||
nr_entries++;
|
nr_entries++;
|
||||||
|
|
||||||
header = kzalloc(sizeof(struct ctl_table_header) +
|
header = kzalloc(sizeof(struct ctl_table_header) +
|
||||||
@@ -1456,7 +1466,7 @@ static int count_subheaders(struct ctl_table *table)
|
|||||||
if (!table || !table->procname)
|
if (!table || !table->procname)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
for (entry = table; entry->procname; entry++) {
|
list_for_each_table_entry(entry, table) {
|
||||||
if (entry->child)
|
if (entry->child)
|
||||||
nr_subheaders += count_subheaders(entry->child);
|
nr_subheaders += count_subheaders(entry->child);
|
||||||
else
|
else
|
||||||
@@ -1475,7 +1485,7 @@ static int register_leaf_sysctl_tables(const char *path, char *pos,
|
|||||||
int nr_dirs = 0;
|
int nr_dirs = 0;
|
||||||
int err = -ENOMEM;
|
int err = -ENOMEM;
|
||||||
|
|
||||||
for (entry = table; entry->procname; entry++) {
|
list_for_each_table_entry(entry, table) {
|
||||||
if (entry->child)
|
if (entry->child)
|
||||||
nr_dirs++;
|
nr_dirs++;
|
||||||
else
|
else
|
||||||
@@ -1492,7 +1502,9 @@ static int register_leaf_sysctl_tables(const char *path, char *pos,
|
|||||||
goto out;
|
goto out;
|
||||||
|
|
||||||
ctl_table_arg = files;
|
ctl_table_arg = files;
|
||||||
for (new = files, entry = table; entry->procname; entry++) {
|
new = files;
|
||||||
|
|
||||||
|
list_for_each_table_entry(entry, table) {
|
||||||
if (entry->child)
|
if (entry->child)
|
||||||
continue;
|
continue;
|
||||||
*new = *entry;
|
*new = *entry;
|
||||||
@@ -1516,7 +1528,7 @@ static int register_leaf_sysctl_tables(const char *path, char *pos,
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Recurse into the subdirectories. */
|
/* Recurse into the subdirectories. */
|
||||||
for (entry = table; entry->procname; entry++) {
|
list_for_each_table_entry(entry, table) {
|
||||||
char *child_pos;
|
char *child_pos;
|
||||||
|
|
||||||
if (!entry->child)
|
if (!entry->child)
|
||||||
@@ -1670,7 +1682,7 @@ static void put_links(struct ctl_table_header *header)
|
|||||||
if (IS_ERR(core_parent))
|
if (IS_ERR(core_parent))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
for (entry = header->ctl_table; entry->procname; entry++) {
|
list_for_each_table_entry(entry, header->ctl_table) {
|
||||||
struct ctl_table_header *link_head;
|
struct ctl_table_header *link_head;
|
||||||
struct ctl_table *link;
|
struct ctl_table *link;
|
||||||
const char *name = entry->procname;
|
const char *name = entry->procname;
|
||||||
|
|||||||
Reference in New Issue
Block a user