module.c 4.62 KB
Newer Older
J. R. Okajima's avatar
J. R. Okajima committed
1
2
3
4
5
6
7
8
9
10
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (C) 2005-2019 Junjiro R. Okajima
 */

/*
 * module global variables and operations
 */

#include <linux/module.h>
J. R. Okajima's avatar
J. R. Okajima committed
11
#include <linux/seq_file.h>
J. R. Okajima's avatar
J. R. Okajima committed
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include "aufs.h"

/* shrinkable realloc */
void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink)
{
	size_t sz;
	int diff;

	sz = 0;
	diff = -1;
	if (p) {
#if 0 /* unused */
		if (!new_sz) {
			au_kfree_rcu(p);
			p = NULL;
			goto out;
		}
#else
		AuDebugOn(!new_sz);
#endif
		sz = ksize(p);
		diff = au_kmidx_sub(sz, new_sz);
	}
	if (sz && !diff)
		goto out;

	if (sz < new_sz)
		/* expand or SLOB */
		p = krealloc(p, new_sz, gfp);
	else if (new_sz < sz && may_shrink) {
		/* shrink */
		void *q;

		q = kmalloc(new_sz, gfp);
		if (q) {
			if (p) {
				memcpy(q, p, new_sz);
				au_kfree_try_rcu(p);
			}
			p = q;
		} else
			p = NULL;
	}

out:
	return p;
}

void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
		   int may_shrink)
{
	p = au_krealloc(p, new_sz, gfp, may_shrink);
	if (p && new_sz > nused)
		memset(p + nused, 0, new_sz - nused);
	return p;
}

/* ---------------------------------------------------------------------- */
J. R. Okajima's avatar
J. R. Okajima committed
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
 * aufs caches
 */
struct kmem_cache *au_cache[AuCache_Last];

static void au_cache_fin(void)
{
	int i;

	/*
	 * Make sure all delayed rcu free inodes are flushed before we
	 * destroy cache.
	 */
	rcu_barrier();
J. R. Okajima's avatar
J. R. Okajima committed
84
85
86
87

	/* excluding AuCache_HNOTIFY */
	BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
	for (i = 0; i < AuCache_HNOTIFY; i++) {
J. R. Okajima's avatar
J. R. Okajima committed
88
89
90
91
92
93
94
		kmem_cache_destroy(au_cache[i]);
		au_cache[i] = NULL;
	}
}

static int __init au_cache_init(void)
{
J. R. Okajima's avatar
J. R. Okajima committed
95
96
97
98
99
	au_cache[AuCache_DINFO] = AuCacheCtor(au_dinfo, au_di_init_once);
	if (au_cache[AuCache_DINFO])
		/* SLAB_DESTROY_BY_RCU */
		au_cache[AuCache_ICNTNR] = AuCacheCtor(au_icntnr,
						       au_icntnr_init_once);
J. R. Okajima's avatar
J. R. Okajima committed
100
	if (au_cache[AuCache_ICNTNR])
J. R. Okajima's avatar
J. R. Okajima committed
101
102
103
		au_cache[AuCache_FINFO] = AuCacheCtor(au_finfo,
						      au_fi_init_once);
	if (au_cache[AuCache_FINFO])
104
105
106
107
		au_cache[AuCache_VDIR] = AuCache(au_vdir);
	if (au_cache[AuCache_VDIR])
		au_cache[AuCache_DEHSTR] = AuCache(au_vdir_dehstr);
	if (au_cache[AuCache_DEHSTR])
J. R. Okajima's avatar
J. R. Okajima committed
108
109
110
111
112
113
114
115
		return 0;

	au_cache_fin();
	return -ENOMEM;
}

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

J. R. Okajima's avatar
J. R. Okajima committed
116
117
int au_dir_roflags;

J. R. Okajima's avatar
J. R. Okajima committed
118
119
120
121
122
123
124
125
#ifdef CONFIG_AUFS_SBILIST
/*
 * iterate_supers_type() doesn't protect us from
 * remounting (branch management)
 */
struct hlist_bl_head au_sbilist;
#endif

J. R. Okajima's avatar
J. R. Okajima committed
126
127
128
129
130
131
132
133
134
135
/*
 * functions for module interface.
 */
MODULE_LICENSE("GPL");
/* MODULE_LICENSE("GPL v2"); */
MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
MODULE_DESCRIPTION(AUFS_NAME
	" -- Advanced multi layered unification filesystem");
MODULE_VERSION(AUFS_VERSION);

J. R. Okajima's avatar
J. R. Okajima committed
136
137
138
139
140
/* this module parameter has no meaning when SYSFS is disabled */
int sysaufs_brs = 1;
MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
module_param_named(brs, sysaufs_brs, int, 0444);

J. R. Okajima's avatar
J. R. Okajima committed
141
142
/* ---------------------------------------------------------------------- */

J. R. Okajima's avatar
J. R. Okajima committed
143
144
145
static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */

int au_seq_path(struct seq_file *seq, struct path *path)
J. R. Okajima's avatar
J. R. Okajima committed
146
{
J. R. Okajima's avatar
J. R. Okajima committed
147
148
	int err;

J. R. Okajima's avatar
J. R. Okajima committed
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
	err = seq_path(seq, path, au_esc_chars);
	if (err >= 0)
		err = 0;
	else
		err = -ENOMEM;

	return err;
}

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

static int __init aufs_init(void)
{
	int err, i;
	char *p;

	p = au_esc_chars;
	for (i = 1; i <= ' '; i++)
		*p++ = i;
	*p++ = '\\';
	*p++ = '\x7f';
	*p = 0;

J. R. Okajima's avatar
J. R. Okajima committed
172
173
	au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);

174
175
176
177
	memcpy(aufs_iop_nogetattr, aufs_iop, sizeof(aufs_iop));
	for (i = 0; i < AuIop_Last; i++)
		aufs_iop_nogetattr[i].getattr = NULL;

J. R. Okajima's avatar
J. R. Okajima committed
178
	memset(au_cache, 0, sizeof(au_cache));	/* including hnotify */
J. R. Okajima's avatar
J. R. Okajima committed
179

J. R. Okajima's avatar
J. R. Okajima committed
180
	au_sbilist_init();
J. R. Okajima's avatar
J. R. Okajima committed
181
	sysaufs_brs_init();
182
	au_debug_init();
183
	au_dy_init();
J. R. Okajima's avatar
J. R. Okajima committed
184
	err = sysaufs_init();
J. R. Okajima's avatar
J. R. Okajima committed
185
186
	if (unlikely(err))
		goto out;
J. R. Okajima's avatar
J. R. Okajima committed
187
	err = dbgaufs_init();
J. R. Okajima's avatar
J. R. Okajima committed
188
189
	if (unlikely(err))
		goto out_sysaufs;
J. R. Okajima's avatar
J. R. Okajima committed
190
191
192
	err = au_procfs_init();
	if (unlikely(err))
		goto out_dbgaufs;
193
194
195
	err = au_wkq_init();
	if (unlikely(err))
		goto out_procfs;
J. R. Okajima's avatar
J. R. Okajima committed
196
	err = au_hnotify_init();
J. R. Okajima's avatar
J. R. Okajima committed
197
198
	if (unlikely(err))
		goto out_wkq;
199
	err = au_sysrq_init();
J. R. Okajima's avatar
J. R. Okajima committed
200
201
	if (unlikely(err))
		goto out_hin;
202
203
204
	err = au_cache_init();
	if (unlikely(err))
		goto out_sysrq;
J. R. Okajima's avatar
J. R. Okajima committed
205

J. R. Okajima's avatar
J. R. Okajima committed
206
207
208
209
	err = register_filesystem(&aufs_fs_type);
	if (unlikely(err))
		goto out_cache;

J. R. Okajima's avatar
J. R. Okajima committed
210
211
	/* since we define pr_fmt, call printk directly */
	printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
J. R. Okajima's avatar
J. R. Okajima committed
212
	goto out; /* success */
J. R. Okajima's avatar
J. R. Okajima committed
213

J. R. Okajima's avatar
J. R. Okajima committed
214
215
out_cache:
	au_cache_fin();
216
217
out_sysrq:
	au_sysrq_fin();
J. R. Okajima's avatar
J. R. Okajima committed
218
219
out_hin:
	au_hnotify_fin();
J. R. Okajima's avatar
J. R. Okajima committed
220
221
out_wkq:
	au_wkq_fin();
222
223
out_procfs:
	au_procfs_fin();
J. R. Okajima's avatar
J. R. Okajima committed
224
225
out_dbgaufs:
	dbgaufs_fin();
J. R. Okajima's avatar
J. R. Okajima committed
226
227
out_sysaufs:
	sysaufs_fin();
228
	au_dy_fin();
J. R. Okajima's avatar
J. R. Okajima committed
229
230
out:
	return err;
J. R. Okajima's avatar
J. R. Okajima committed
231
232
233
}

module_init(aufs_init);