procfs.c 3.01 KB
Newer Older
1
2
// SPDX-License-Identifier: GPL-2.0
/*
3
 * Copyright (C) 2010-2020 Junjiro R. Okajima
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
 */

/*
 * procfs interfaces
 */

#include <linux/proc_fs.h>
#include "aufs.h"

static int au_procfs_plm_release(struct inode *inode, struct file *file)
{
	struct au_sbinfo *sbinfo;

	sbinfo = file->private_data;
	if (sbinfo) {
		au_plink_maint_leave(sbinfo);
		kobject_put(&sbinfo->si_kobj);
	}

	return 0;
}

static void au_procfs_plm_write_clean(struct file *file)
{
	struct au_sbinfo *sbinfo;

	sbinfo = file->private_data;
	if (sbinfo)
		au_plink_clean(sbinfo->si_sb, /*verbose*/0);
}

static int au_procfs_plm_write_si(struct file *file, unsigned long id)
{
	int err;
	struct super_block *sb;
	struct au_sbinfo *sbinfo;
	struct hlist_bl_node *pos;

	err = -EBUSY;
	if (unlikely(file->private_data))
		goto out;

	sb = NULL;
	/* don't use au_sbilist_lock() here */
	hlist_bl_lock(&au_sbilist);
	hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
		if (id == sysaufs_si_id(sbinfo)) {
51
52
			if (kobject_get_unless_zero(&sbinfo->si_kobj))
				sb = sbinfo->si_sb;
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
			break;
		}
	hlist_bl_unlock(&au_sbilist);

	err = -EINVAL;
	if (unlikely(!sb))
		goto out;

	err = au_plink_maint_enter(sb);
	if (!err)
		/* keep kobject_get() */
		file->private_data = sbinfo;
	else
		kobject_put(&sbinfo->si_kobj);
out:
	return err;
}

/*
 * Accept a valid "si=xxxx" only.
 * Once it is accepted successfully, accept "clean" too.
 */
static ssize_t au_procfs_plm_write(struct file *file, const char __user *ubuf,
				   size_t count, loff_t *ppos)
{
	ssize_t err;
	unsigned long id;
	/* last newline is allowed */
	char buf[3 + sizeof(unsigned long) * 2 + 1];

	err = -EACCES;
	if (unlikely(!capable(CAP_SYS_ADMIN)))
		goto out;

	err = -EINVAL;
	if (unlikely(count > sizeof(buf)))
		goto out;

	err = copy_from_user(buf, ubuf, count);
	if (unlikely(err)) {
		err = -EFAULT;
		goto out;
	}
	buf[count] = 0;

	err = -EINVAL;
	if (!strcmp("clean", buf)) {
		au_procfs_plm_write_clean(file);
		goto out_success;
	} else if (unlikely(strncmp("si=", buf, 3)))
		goto out;

	err = kstrtoul(buf + 3, 16, &id);
	if (unlikely(err))
		goto out;

	err = au_procfs_plm_write_si(file, id);
	if (unlikely(err))
		goto out;

out_success:
	err = count; /* success */
out:
	return err;
}

119
120
121
static const struct proc_ops au_procfs_plm_op = {
	.proc_write	= au_procfs_plm_write,
	.proc_release	= au_procfs_plm_release
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
};

/* ---------------------------------------------------------------------- */

static struct proc_dir_entry *au_procfs_dir;

void au_procfs_fin(void)
{
	remove_proc_entry(AUFS_PLINK_MAINT_NAME, au_procfs_dir);
	remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
}

int __init au_procfs_init(void)
{
	int err;
	struct proc_dir_entry *entry;

	err = -ENOMEM;
	au_procfs_dir = proc_mkdir(AUFS_PLINK_MAINT_DIR, NULL);
	if (unlikely(!au_procfs_dir))
		goto out;

	entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | 0200,
145
			    au_procfs_dir, &au_procfs_plm_op);
146
147
148
149
150
151
152
153
154
155
156
157
	if (unlikely(!entry))
		goto out_dir;

	err = 0;
	goto out; /* success */


out_dir:
	remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
out:
	return err;
}