]>
Commit | Line | Data |
---|---|---|
c088e31d SF |
1 | /* |
2 | * Copyright (C) 2005-2017 Junjiro R. Okajima | |
3 | * | |
4 | * This program, aufs is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation; either version 2 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
16 | */ | |
17 | ||
18 | /* | |
19 | * module global variables and operations | |
20 | */ | |
21 | ||
22 | #include <linux/module.h> | |
23 | #include <linux/seq_file.h> | |
24 | #include "aufs.h" | |
25 | ||
26 | /* shrinkable realloc */ | |
27 | void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink) | |
28 | { | |
29 | size_t sz; | |
30 | int diff; | |
31 | ||
32 | sz = 0; | |
33 | diff = -1; | |
34 | if (p) { | |
35 | #if 0 /* unused */ | |
36 | if (!new_sz) { | |
37 | kfree(p); | |
38 | p = NULL; | |
39 | goto out; | |
40 | } | |
41 | #else | |
42 | AuDebugOn(!new_sz); | |
43 | #endif | |
44 | sz = ksize(p); | |
45 | diff = au_kmidx_sub(sz, new_sz); | |
46 | } | |
47 | if (sz && !diff) | |
48 | goto out; | |
49 | ||
50 | if (sz < new_sz) | |
51 | /* expand or SLOB */ | |
52 | p = krealloc(p, new_sz, gfp); | |
53 | else if (new_sz < sz && may_shrink) { | |
54 | /* shrink */ | |
55 | void *q; | |
56 | ||
57 | q = kmalloc(new_sz, gfp); | |
58 | if (q) { | |
59 | if (p) { | |
60 | memcpy(q, p, new_sz); | |
61 | kfree(p); | |
62 | } | |
63 | p = q; | |
64 | } else | |
65 | p = NULL; | |
66 | } | |
67 | ||
68 | out: | |
69 | return p; | |
70 | } | |
71 | ||
72 | void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp, | |
73 | int may_shrink) | |
74 | { | |
75 | p = au_krealloc(p, new_sz, gfp, may_shrink); | |
76 | if (p && new_sz > nused) | |
77 | memset(p + nused, 0, new_sz - nused); | |
78 | return p; | |
79 | } | |
80 | ||
81 | /* ---------------------------------------------------------------------- */ | |
82 | /* | |
83 | * aufs caches | |
84 | */ | |
85 | struct kmem_cache *au_cache[AuCache_Last]; | |
86 | ||
87 | static void au_cache_fin(void) | |
88 | { | |
89 | int i; | |
90 | ||
91 | /* | |
92 | * Make sure all delayed rcu free inodes are flushed before we | |
93 | * destroy cache. | |
94 | */ | |
95 | rcu_barrier(); | |
96 | ||
97 | /* excluding AuCache_HNOTIFY */ | |
98 | BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last); | |
99 | for (i = 0; i < AuCache_HNOTIFY; i++) { | |
100 | kmem_cache_destroy(au_cache[i]); | |
101 | au_cache[i] = NULL; | |
102 | } | |
103 | } | |
104 | ||
105 | static int __init au_cache_init(void) | |
106 | { | |
107 | au_cache[AuCache_DINFO] = AuCacheCtor(au_dinfo, au_di_init_once); | |
108 | if (au_cache[AuCache_DINFO]) | |
109 | /* SLAB_DESTROY_BY_RCU */ | |
110 | au_cache[AuCache_ICNTNR] = AuCacheCtor(au_icntnr, | |
111 | au_icntnr_init_once); | |
112 | if (au_cache[AuCache_ICNTNR]) | |
113 | au_cache[AuCache_FINFO] = AuCacheCtor(au_finfo, | |
114 | au_fi_init_once); | |
115 | if (au_cache[AuCache_FINFO]) | |
116 | au_cache[AuCache_VDIR] = AuCache(au_vdir); | |
117 | if (au_cache[AuCache_VDIR]) | |
118 | au_cache[AuCache_DEHSTR] = AuCache(au_vdir_dehstr); | |
119 | if (au_cache[AuCache_DEHSTR]) | |
120 | return 0; | |
121 | ||
122 | au_cache_fin(); | |
123 | return -ENOMEM; | |
124 | } | |
125 | ||
126 | /* ---------------------------------------------------------------------- */ | |
127 | ||
128 | int au_dir_roflags; | |
129 | ||
130 | #ifdef CONFIG_AUFS_SBILIST | |
131 | /* | |
132 | * iterate_supers_type() doesn't protect us from | |
133 | * remounting (branch management) | |
134 | */ | |
135 | struct hlist_bl_head au_sbilist; | |
136 | #endif | |
137 | ||
138 | /* | |
139 | * functions for module interface. | |
140 | */ | |
141 | MODULE_LICENSE("GPL"); | |
142 | /* MODULE_LICENSE("GPL v2"); */ | |
143 | MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>"); | |
144 | MODULE_DESCRIPTION(AUFS_NAME | |
145 | " -- Advanced multi layered unification filesystem"); | |
146 | MODULE_VERSION(AUFS_VERSION); | |
147 | MODULE_ALIAS_FS(AUFS_NAME); | |
148 | ||
149 | /* this module parameter has no meaning when SYSFS is disabled */ | |
150 | int sysaufs_brs = 1; | |
151 | MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN"); | |
152 | module_param_named(brs, sysaufs_brs, int, S_IRUGO); | |
153 | ||
154 | /* this module parameter has no meaning when USER_NS is disabled */ | |
155 | bool au_userns; | |
156 | MODULE_PARM_DESC(allow_userns, "allow unprivileged to mount under userns"); | |
157 | module_param_named(allow_userns, au_userns, bool, S_IRUGO); | |
158 | ||
159 | /* ---------------------------------------------------------------------- */ | |
160 | ||
161 | static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */ | |
162 | ||
163 | int au_seq_path(struct seq_file *seq, struct path *path) | |
164 | { | |
165 | int err; | |
166 | ||
167 | err = seq_path(seq, path, au_esc_chars); | |
168 | if (err >= 0) | |
169 | err = 0; | |
170 | else | |
171 | err = -ENOMEM; | |
172 | ||
173 | return err; | |
174 | } | |
175 | ||
176 | /* ---------------------------------------------------------------------- */ | |
177 | ||
178 | static int __init aufs_init(void) | |
179 | { | |
180 | int err, i; | |
181 | char *p; | |
182 | ||
183 | p = au_esc_chars; | |
184 | for (i = 1; i <= ' '; i++) | |
185 | *p++ = i; | |
186 | *p++ = '\\'; | |
187 | *p++ = '\x7f'; | |
188 | *p = 0; | |
189 | ||
190 | au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE); | |
191 | ||
192 | memcpy(aufs_iop_nogetattr, aufs_iop, sizeof(aufs_iop)); | |
193 | for (i = 0; i < AuIop_Last; i++) | |
194 | aufs_iop_nogetattr[i].getattr = NULL; | |
195 | ||
196 | memset(au_cache, 0, sizeof(au_cache)); /* including hnotify */ | |
197 | ||
198 | au_sbilist_init(); | |
199 | sysaufs_brs_init(); | |
200 | au_debug_init(); | |
201 | au_dy_init(); | |
202 | err = sysaufs_init(); | |
203 | if (unlikely(err)) | |
204 | goto out; | |
205 | err = au_procfs_init(); | |
206 | if (unlikely(err)) | |
207 | goto out_sysaufs; | |
208 | err = au_wkq_init(); | |
209 | if (unlikely(err)) | |
210 | goto out_procfs; | |
211 | err = au_loopback_init(); | |
212 | if (unlikely(err)) | |
213 | goto out_wkq; | |
214 | err = au_hnotify_init(); | |
215 | if (unlikely(err)) | |
216 | goto out_loopback; | |
217 | err = au_sysrq_init(); | |
218 | if (unlikely(err)) | |
219 | goto out_hin; | |
220 | err = au_cache_init(); | |
221 | if (unlikely(err)) | |
222 | goto out_sysrq; | |
223 | ||
224 | aufs_fs_type.fs_flags |= au_userns ? FS_USERNS_MOUNT : 0; | |
225 | err = register_filesystem(&aufs_fs_type); | |
226 | if (unlikely(err)) | |
227 | goto out_cache; | |
228 | ||
229 | /* since we define pr_fmt, call printk directly */ | |
230 | printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n"); | |
231 | goto out; /* success */ | |
232 | ||
233 | out_cache: | |
234 | au_cache_fin(); | |
235 | out_sysrq: | |
236 | au_sysrq_fin(); | |
237 | out_hin: | |
238 | au_hnotify_fin(); | |
239 | out_loopback: | |
240 | au_loopback_fin(); | |
241 | out_wkq: | |
242 | au_wkq_fin(); | |
243 | out_procfs: | |
244 | au_procfs_fin(); | |
245 | out_sysaufs: | |
246 | sysaufs_fin(); | |
247 | au_dy_fin(); | |
248 | out: | |
249 | return err; | |
250 | } | |
251 | ||
252 | static void __exit aufs_exit(void) | |
253 | { | |
254 | unregister_filesystem(&aufs_fs_type); | |
255 | au_cache_fin(); | |
256 | au_sysrq_fin(); | |
257 | au_hnotify_fin(); | |
258 | au_loopback_fin(); | |
259 | au_wkq_fin(); | |
260 | au_procfs_fin(); | |
261 | sysaufs_fin(); | |
262 | au_dy_fin(); | |
263 | } | |
264 | ||
265 | module_init(aufs_init); | |
266 | module_exit(aufs_exit); |