]>
Commit | Line | Data |
---|---|---|
e9be9d5e MS |
1 | /* |
2 | * | |
3 | * Copyright (C) 2011 Novell Inc. | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify it | |
6 | * under the terms of the GNU General Public License version 2 as published by | |
7 | * the Free Software Foundation. | |
8 | */ | |
9 | ||
10 | #include <linux/fs.h> | |
11 | #include <linux/namei.h> | |
cf9a6784 | 12 | #include <linux/pagemap.h> |
e9be9d5e MS |
13 | #include <linux/xattr.h> |
14 | #include <linux/security.h> | |
15 | #include <linux/mount.h> | |
16 | #include <linux/slab.h> | |
17 | #include <linux/parser.h> | |
18 | #include <linux/module.h> | |
e458bcd1 | 19 | #include <linux/pagemap.h> |
e9be9d5e | 20 | #include <linux/sched.h> |
cc259639 | 21 | #include <linux/statfs.h> |
f45827e8 | 22 | #include <linux/seq_file.h> |
e9be9d5e MS |
23 | #include "overlayfs.h" |
24 | ||
25 | MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>"); | |
26 | MODULE_DESCRIPTION("Overlay filesystem"); | |
27 | MODULE_LICENSE("GPL"); | |
28 | ||
f45827e8 EZ |
29 | struct ovl_config { |
30 | char *lowerdir; | |
31 | char *upperdir; | |
32 | char *workdir; | |
8d3095f4 | 33 | bool default_permissions; |
f45827e8 EZ |
34 | }; |
35 | ||
e9be9d5e MS |
36 | /* private information held for overlayfs's superblock */ |
37 | struct ovl_fs { | |
38 | struct vfsmount *upper_mnt; | |
dd662667 MS |
39 | unsigned numlower; |
40 | struct vfsmount **lower_mnt; | |
e9be9d5e | 41 | struct dentry *workdir; |
cc259639 | 42 | long lower_namelen; |
f45827e8 EZ |
43 | /* pathnames of lower and upper dirs, for show_options */ |
44 | struct ovl_config config; | |
3fe6e52f AM |
45 | /* creds of process who forced instantiation of super block */ |
46 | const struct cred *creator_cred; | |
e9be9d5e MS |
47 | }; |
48 | ||
49 | struct ovl_dir_cache; | |
50 | ||
51 | /* private information held for every overlayfs dentry */ | |
52 | struct ovl_entry { | |
53 | struct dentry *__upperdentry; | |
e9be9d5e MS |
54 | struct ovl_dir_cache *cache; |
55 | union { | |
56 | struct { | |
57 | u64 version; | |
58 | bool opaque; | |
59 | }; | |
60 | struct rcu_head rcu; | |
61 | }; | |
dd662667 MS |
62 | unsigned numlower; |
63 | struct path lowerstack[]; | |
e9be9d5e MS |
64 | }; |
65 | ||
a78d9f0d MS |
66 | #define OVL_MAX_STACK 500 |
67 | ||
dd662667 MS |
68 | static struct dentry *__ovl_dentry_lower(struct ovl_entry *oe) |
69 | { | |
70 | return oe->numlower ? oe->lowerstack[0].dentry : NULL; | |
71 | } | |
e9be9d5e MS |
72 | |
73 | enum ovl_path_type ovl_path_type(struct dentry *dentry) | |
74 | { | |
75 | struct ovl_entry *oe = dentry->d_fsdata; | |
1afaba1e | 76 | enum ovl_path_type type = 0; |
e9be9d5e MS |
77 | |
78 | if (oe->__upperdentry) { | |
1afaba1e MS |
79 | type = __OVL_PATH_UPPER; |
80 | ||
45d11738 KK |
81 | /* |
82 | * Non-dir dentry can hold lower dentry from previous | |
83 | * location. Its purity depends only on opaque flag. | |
84 | */ | |
85 | if (oe->numlower && S_ISDIR(dentry->d_inode->i_mode)) | |
86 | type |= __OVL_PATH_MERGE; | |
87 | else if (!oe->opaque) | |
1afaba1e | 88 | type |= __OVL_PATH_PURE; |
9d7459d8 MS |
89 | } else { |
90 | if (oe->numlower > 1) | |
91 | type |= __OVL_PATH_MERGE; | |
e9be9d5e | 92 | } |
1afaba1e | 93 | return type; |
e9be9d5e MS |
94 | } |
95 | ||
96 | static struct dentry *ovl_upperdentry_dereference(struct ovl_entry *oe) | |
97 | { | |
71d50928 | 98 | return lockless_dereference(oe->__upperdentry); |
e9be9d5e MS |
99 | } |
100 | ||
101 | void ovl_path_upper(struct dentry *dentry, struct path *path) | |
102 | { | |
103 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; | |
104 | struct ovl_entry *oe = dentry->d_fsdata; | |
105 | ||
106 | path->mnt = ofs->upper_mnt; | |
107 | path->dentry = ovl_upperdentry_dereference(oe); | |
108 | } | |
109 | ||
110 | enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path) | |
111 | { | |
e9be9d5e MS |
112 | enum ovl_path_type type = ovl_path_type(dentry); |
113 | ||
1afaba1e | 114 | if (!OVL_TYPE_UPPER(type)) |
e9be9d5e MS |
115 | ovl_path_lower(dentry, path); |
116 | else | |
117 | ovl_path_upper(dentry, path); | |
118 | ||
119 | return type; | |
120 | } | |
121 | ||
122 | struct dentry *ovl_dentry_upper(struct dentry *dentry) | |
123 | { | |
124 | struct ovl_entry *oe = dentry->d_fsdata; | |
125 | ||
126 | return ovl_upperdentry_dereference(oe); | |
127 | } | |
128 | ||
129 | struct dentry *ovl_dentry_lower(struct dentry *dentry) | |
130 | { | |
131 | struct ovl_entry *oe = dentry->d_fsdata; | |
132 | ||
dd662667 | 133 | return __ovl_dentry_lower(oe); |
e9be9d5e MS |
134 | } |
135 | ||
136 | struct dentry *ovl_dentry_real(struct dentry *dentry) | |
137 | { | |
138 | struct ovl_entry *oe = dentry->d_fsdata; | |
139 | struct dentry *realdentry; | |
140 | ||
141 | realdentry = ovl_upperdentry_dereference(oe); | |
142 | if (!realdentry) | |
dd662667 | 143 | realdentry = __ovl_dentry_lower(oe); |
e9be9d5e MS |
144 | |
145 | return realdentry; | |
146 | } | |
147 | ||
39b681f8 MS |
148 | static void ovl_inode_init(struct inode *inode, struct inode *realinode, |
149 | bool is_upper) | |
e9be9d5e | 150 | { |
39b681f8 MS |
151 | WRITE_ONCE(inode->i_private, (unsigned long) realinode | |
152 | (is_upper ? OVL_ISUPPER_MASK : 0)); | |
39a25b2b VG |
153 | } |
154 | ||
8d3095f4 MS |
155 | struct vfsmount *ovl_entry_mnt_real(struct ovl_entry *oe, struct inode *inode, |
156 | bool is_upper) | |
157 | { | |
158 | if (is_upper) { | |
159 | struct ovl_fs *ofs = inode->i_sb->s_fs_info; | |
160 | ||
161 | return ofs->upper_mnt; | |
162 | } else { | |
163 | return oe->numlower ? oe->lowerstack[0].mnt : NULL; | |
164 | } | |
165 | } | |
166 | ||
e9be9d5e MS |
167 | struct ovl_dir_cache *ovl_dir_cache(struct dentry *dentry) |
168 | { | |
169 | struct ovl_entry *oe = dentry->d_fsdata; | |
170 | ||
171 | return oe->cache; | |
172 | } | |
173 | ||
174 | void ovl_set_dir_cache(struct dentry *dentry, struct ovl_dir_cache *cache) | |
175 | { | |
176 | struct ovl_entry *oe = dentry->d_fsdata; | |
177 | ||
178 | oe->cache = cache; | |
179 | } | |
180 | ||
181 | void ovl_path_lower(struct dentry *dentry, struct path *path) | |
182 | { | |
e9be9d5e MS |
183 | struct ovl_entry *oe = dentry->d_fsdata; |
184 | ||
dd662667 | 185 | *path = oe->numlower ? oe->lowerstack[0] : (struct path) { NULL, NULL }; |
e9be9d5e MS |
186 | } |
187 | ||
188 | int ovl_want_write(struct dentry *dentry) | |
189 | { | |
190 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; | |
191 | return mnt_want_write(ofs->upper_mnt); | |
192 | } | |
193 | ||
194 | void ovl_drop_write(struct dentry *dentry) | |
195 | { | |
196 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; | |
197 | mnt_drop_write(ofs->upper_mnt); | |
198 | } | |
199 | ||
200 | struct dentry *ovl_workdir(struct dentry *dentry) | |
201 | { | |
202 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; | |
203 | return ofs->workdir; | |
204 | } | |
205 | ||
206 | bool ovl_dentry_is_opaque(struct dentry *dentry) | |
207 | { | |
208 | struct ovl_entry *oe = dentry->d_fsdata; | |
209 | return oe->opaque; | |
210 | } | |
211 | ||
212 | void ovl_dentry_set_opaque(struct dentry *dentry, bool opaque) | |
213 | { | |
214 | struct ovl_entry *oe = dentry->d_fsdata; | |
215 | oe->opaque = opaque; | |
216 | } | |
217 | ||
218 | void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry) | |
219 | { | |
220 | struct ovl_entry *oe = dentry->d_fsdata; | |
221 | ||
5955102c | 222 | WARN_ON(!inode_is_locked(upperdentry->d_parent->d_inode)); |
e9be9d5e | 223 | WARN_ON(oe->__upperdentry); |
e9be9d5e MS |
224 | /* |
225 | * Make sure upperdentry is consistent before making it visible to | |
226 | * ovl_upperdentry_dereference(). | |
227 | */ | |
228 | smp_wmb(); | |
229 | oe->__upperdentry = upperdentry; | |
230 | } | |
231 | ||
39b681f8 MS |
232 | void ovl_inode_update(struct inode *inode, struct inode *upperinode) |
233 | { | |
234 | WARN_ON(!upperinode); | |
235 | WRITE_ONCE(inode->i_private, | |
236 | (unsigned long) upperinode | OVL_ISUPPER_MASK); | |
237 | } | |
238 | ||
e9be9d5e MS |
239 | void ovl_dentry_version_inc(struct dentry *dentry) |
240 | { | |
241 | struct ovl_entry *oe = dentry->d_fsdata; | |
242 | ||
5955102c | 243 | WARN_ON(!inode_is_locked(dentry->d_inode)); |
e9be9d5e MS |
244 | oe->version++; |
245 | } | |
246 | ||
247 | u64 ovl_dentry_version_get(struct dentry *dentry) | |
248 | { | |
249 | struct ovl_entry *oe = dentry->d_fsdata; | |
250 | ||
5955102c | 251 | WARN_ON(!inode_is_locked(dentry->d_inode)); |
e9be9d5e MS |
252 | return oe->version; |
253 | } | |
254 | ||
255 | bool ovl_is_whiteout(struct dentry *dentry) | |
256 | { | |
257 | struct inode *inode = dentry->d_inode; | |
258 | ||
259 | return inode && IS_WHITEOUT(inode); | |
260 | } | |
261 | ||
3fe6e52f AM |
262 | const struct cred *ovl_override_creds(struct super_block *sb) |
263 | { | |
264 | struct ovl_fs *ofs = sb->s_fs_info; | |
265 | ||
266 | return override_creds(ofs->creator_cred); | |
267 | } | |
268 | ||
e9be9d5e MS |
269 | static bool ovl_is_opaquedir(struct dentry *dentry) |
270 | { | |
271 | int res; | |
272 | char val; | |
273 | struct inode *inode = dentry->d_inode; | |
274 | ||
275 | if (!S_ISDIR(inode->i_mode) || !inode->i_op->getxattr) | |
276 | return false; | |
277 | ||
ce23e640 | 278 | res = inode->i_op->getxattr(dentry, inode, OVL_XATTR_OPAQUE, &val, 1); |
e9be9d5e MS |
279 | if (res == 1 && val == 'y') |
280 | return true; | |
281 | ||
282 | return false; | |
283 | } | |
284 | ||
285 | static void ovl_dentry_release(struct dentry *dentry) | |
286 | { | |
287 | struct ovl_entry *oe = dentry->d_fsdata; | |
288 | ||
289 | if (oe) { | |
dd662667 MS |
290 | unsigned int i; |
291 | ||
e9be9d5e | 292 | dput(oe->__upperdentry); |
dd662667 MS |
293 | for (i = 0; i < oe->numlower; i++) |
294 | dput(oe->lowerstack[i].dentry); | |
e9be9d5e MS |
295 | kfree_rcu(oe, rcu); |
296 | } | |
297 | } | |
298 | ||
2d902671 MS |
299 | static struct dentry *ovl_d_real(struct dentry *dentry, |
300 | const struct inode *inode, | |
301 | unsigned int open_flags) | |
d101a125 MS |
302 | { |
303 | struct dentry *real; | |
304 | ||
305 | if (d_is_dir(dentry)) { | |
306 | if (!inode || inode == d_inode(dentry)) | |
307 | return dentry; | |
308 | goto bug; | |
309 | } | |
310 | ||
2d902671 MS |
311 | if (d_is_negative(dentry)) |
312 | return dentry; | |
313 | ||
314 | if (open_flags) { | |
315 | int err = ovl_open_maybe_copy_up(dentry, open_flags); | |
316 | ||
317 | if (err) | |
318 | return ERR_PTR(err); | |
319 | } | |
320 | ||
d101a125 MS |
321 | real = ovl_dentry_upper(dentry); |
322 | if (real && (!inode || inode == d_inode(real))) | |
323 | return real; | |
324 | ||
325 | real = ovl_dentry_lower(dentry); | |
326 | if (!real) | |
327 | goto bug; | |
328 | ||
329 | if (!inode || inode == d_inode(real)) | |
330 | return real; | |
331 | ||
332 | /* Handle recursion */ | |
2d902671 | 333 | return d_real(real, inode, open_flags); |
d101a125 MS |
334 | bug: |
335 | WARN(1, "ovl_d_real(%pd4, %s:%lu\n): real dentry not found\n", dentry, | |
336 | inode ? inode->i_sb->s_id : "NULL", inode ? inode->i_ino : 0); | |
337 | return dentry; | |
338 | } | |
339 | ||
7c03b5d4 MS |
340 | static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags) |
341 | { | |
342 | struct ovl_entry *oe = dentry->d_fsdata; | |
343 | unsigned int i; | |
344 | int ret = 1; | |
345 | ||
346 | for (i = 0; i < oe->numlower; i++) { | |
347 | struct dentry *d = oe->lowerstack[i].dentry; | |
348 | ||
349 | if (d->d_flags & DCACHE_OP_REVALIDATE) { | |
350 | ret = d->d_op->d_revalidate(d, flags); | |
351 | if (ret < 0) | |
352 | return ret; | |
353 | if (!ret) { | |
354 | if (!(flags & LOOKUP_RCU)) | |
355 | d_invalidate(d); | |
356 | return -ESTALE; | |
357 | } | |
358 | } | |
359 | } | |
360 | return 1; | |
361 | } | |
362 | ||
363 | static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags) | |
364 | { | |
365 | struct ovl_entry *oe = dentry->d_fsdata; | |
366 | unsigned int i; | |
367 | int ret = 1; | |
368 | ||
369 | for (i = 0; i < oe->numlower; i++) { | |
370 | struct dentry *d = oe->lowerstack[i].dentry; | |
371 | ||
372 | if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE) { | |
373 | ret = d->d_op->d_weak_revalidate(d, flags); | |
374 | if (ret <= 0) | |
375 | break; | |
376 | } | |
377 | } | |
378 | return ret; | |
379 | } | |
380 | ||
e9be9d5e MS |
381 | static const struct dentry_operations ovl_dentry_operations = { |
382 | .d_release = ovl_dentry_release, | |
d101a125 | 383 | .d_real = ovl_d_real, |
e9be9d5e MS |
384 | }; |
385 | ||
7c03b5d4 MS |
386 | static const struct dentry_operations ovl_reval_dentry_operations = { |
387 | .d_release = ovl_dentry_release, | |
d101a125 | 388 | .d_real = ovl_d_real, |
7c03b5d4 MS |
389 | .d_revalidate = ovl_dentry_revalidate, |
390 | .d_weak_revalidate = ovl_dentry_weak_revalidate, | |
391 | }; | |
392 | ||
dd662667 | 393 | static struct ovl_entry *ovl_alloc_entry(unsigned int numlower) |
e9be9d5e | 394 | { |
dd662667 MS |
395 | size_t size = offsetof(struct ovl_entry, lowerstack[numlower]); |
396 | struct ovl_entry *oe = kzalloc(size, GFP_KERNEL); | |
397 | ||
398 | if (oe) | |
399 | oe->numlower = numlower; | |
400 | ||
401 | return oe; | |
e9be9d5e MS |
402 | } |
403 | ||
7c03b5d4 MS |
404 | static bool ovl_dentry_remote(struct dentry *dentry) |
405 | { | |
406 | return dentry->d_flags & | |
407 | (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE); | |
408 | } | |
409 | ||
410 | static bool ovl_dentry_weird(struct dentry *dentry) | |
411 | { | |
412 | return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT | | |
413 | DCACHE_MANAGE_TRANSIT | | |
414 | DCACHE_OP_HASH | | |
415 | DCACHE_OP_COMPARE); | |
416 | } | |
417 | ||
c1b2cc1a MS |
418 | static inline struct dentry *ovl_lookup_real(struct super_block *ovl_sb, |
419 | struct dentry *dir, | |
e9be9d5e MS |
420 | struct qstr *name) |
421 | { | |
c1b2cc1a | 422 | const struct cred *old_cred; |
e9be9d5e MS |
423 | struct dentry *dentry; |
424 | ||
c1b2cc1a MS |
425 | old_cred = ovl_override_creds(ovl_sb); |
426 | dentry = lookup_one_len_unlocked(name->name, dir, name->len); | |
427 | revert_creds(old_cred); | |
e9be9d5e MS |
428 | |
429 | if (IS_ERR(dentry)) { | |
430 | if (PTR_ERR(dentry) == -ENOENT) | |
431 | dentry = NULL; | |
432 | } else if (!dentry->d_inode) { | |
433 | dput(dentry); | |
434 | dentry = NULL; | |
7c03b5d4 | 435 | } else if (ovl_dentry_weird(dentry)) { |
a6f15d9a | 436 | dput(dentry); |
7c03b5d4 | 437 | /* Don't support traversing automounts and other weirdness */ |
a6f15d9a | 438 | dentry = ERR_PTR(-EREMOTE); |
e9be9d5e MS |
439 | } |
440 | return dentry; | |
441 | } | |
442 | ||
5ef88da5 MS |
443 | /* |
444 | * Returns next layer in stack starting from top. | |
445 | * Returns -1 if this is the last layer. | |
446 | */ | |
447 | int ovl_path_next(int idx, struct dentry *dentry, struct path *path) | |
448 | { | |
449 | struct ovl_entry *oe = dentry->d_fsdata; | |
450 | ||
451 | BUG_ON(idx < 0); | |
452 | if (idx == 0) { | |
453 | ovl_path_upper(dentry, path); | |
454 | if (path->dentry) | |
455 | return oe->numlower ? 1 : -1; | |
456 | idx++; | |
457 | } | |
458 | BUG_ON(idx > oe->numlower); | |
459 | *path = oe->lowerstack[idx - 1]; | |
460 | ||
461 | return (idx < oe->numlower) ? idx + 1 : -1; | |
462 | } | |
463 | ||
e9be9d5e MS |
464 | struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry, |
465 | unsigned int flags) | |
466 | { | |
467 | struct ovl_entry *oe; | |
3d3c6b89 MS |
468 | struct ovl_entry *poe = dentry->d_parent->d_fsdata; |
469 | struct path *stack = NULL; | |
470 | struct dentry *upperdir, *upperdentry = NULL; | |
471 | unsigned int ctr = 0; | |
e9be9d5e | 472 | struct inode *inode = NULL; |
3d3c6b89 MS |
473 | bool upperopaque = false; |
474 | struct dentry *this, *prev = NULL; | |
475 | unsigned int i; | |
e9be9d5e MS |
476 | int err; |
477 | ||
3d3c6b89 | 478 | upperdir = ovl_upperdentry_dereference(poe); |
e9be9d5e | 479 | if (upperdir) { |
c1b2cc1a | 480 | this = ovl_lookup_real(dentry->d_sb, upperdir, &dentry->d_name); |
3d3c6b89 MS |
481 | err = PTR_ERR(this); |
482 | if (IS_ERR(this)) | |
483 | goto out; | |
484 | ||
3e01cee3 | 485 | if (this) { |
7c03b5d4 MS |
486 | if (unlikely(ovl_dentry_remote(this))) { |
487 | dput(this); | |
488 | err = -EREMOTE; | |
489 | goto out; | |
490 | } | |
3d3c6b89 MS |
491 | if (ovl_is_whiteout(this)) { |
492 | dput(this); | |
493 | this = NULL; | |
494 | upperopaque = true; | |
3e01cee3 | 495 | } else if (poe->numlower && ovl_is_opaquedir(this)) { |
3d3c6b89 | 496 | upperopaque = true; |
e9be9d5e MS |
497 | } |
498 | } | |
3d3c6b89 | 499 | upperdentry = prev = this; |
e9be9d5e | 500 | } |
3d3c6b89 MS |
501 | |
502 | if (!upperopaque && poe->numlower) { | |
503 | err = -ENOMEM; | |
504 | stack = kcalloc(poe->numlower, sizeof(struct path), GFP_KERNEL); | |
505 | if (!stack) | |
506 | goto out_put_upper; | |
e9be9d5e MS |
507 | } |
508 | ||
3d3c6b89 MS |
509 | for (i = 0; !upperopaque && i < poe->numlower; i++) { |
510 | bool opaque = false; | |
511 | struct path lowerpath = poe->lowerstack[i]; | |
512 | ||
c1b2cc1a MS |
513 | this = ovl_lookup_real(dentry->d_sb, |
514 | lowerpath.dentry, &dentry->d_name); | |
3d3c6b89 | 515 | err = PTR_ERR(this); |
09e10322 MS |
516 | if (IS_ERR(this)) { |
517 | /* | |
518 | * If it's positive, then treat ENAMETOOLONG as ENOENT. | |
519 | */ | |
520 | if (err == -ENAMETOOLONG && (upperdentry || ctr)) | |
521 | continue; | |
3d3c6b89 | 522 | goto out_put; |
09e10322 | 523 | } |
3d3c6b89 MS |
524 | if (!this) |
525 | continue; | |
3e01cee3 MS |
526 | if (ovl_is_whiteout(this)) { |
527 | dput(this); | |
528 | break; | |
529 | } | |
3d3c6b89 | 530 | /* |
3e01cee3 MS |
531 | * Only makes sense to check opaque dir if this is not the |
532 | * lowermost layer. | |
3d3c6b89 | 533 | */ |
3e01cee3 MS |
534 | if (i < poe->numlower - 1 && ovl_is_opaquedir(this)) |
535 | opaque = true; | |
a425c037 | 536 | |
537 | if (prev && (!S_ISDIR(prev->d_inode->i_mode) || | |
538 | !S_ISDIR(this->d_inode->i_mode))) { | |
539 | /* | |
540 | * FIXME: check for upper-opaqueness maybe better done | |
541 | * in remove code. | |
542 | */ | |
3d3c6b89 MS |
543 | if (prev == upperdentry) |
544 | upperopaque = true; | |
545 | dput(this); | |
546 | break; | |
547 | } | |
a425c037 | 548 | /* |
549 | * If this is a non-directory then stop here. | |
550 | */ | |
551 | if (!S_ISDIR(this->d_inode->i_mode)) | |
552 | opaque = true; | |
553 | ||
3d3c6b89 MS |
554 | stack[ctr].dentry = this; |
555 | stack[ctr].mnt = lowerpath.mnt; | |
556 | ctr++; | |
557 | prev = this; | |
558 | if (opaque) | |
559 | break; | |
e9be9d5e MS |
560 | } |
561 | ||
3d3c6b89 MS |
562 | oe = ovl_alloc_entry(ctr); |
563 | err = -ENOMEM; | |
564 | if (!oe) | |
565 | goto out_put; | |
566 | ||
567 | if (upperdentry || ctr) { | |
e9be9d5e | 568 | struct dentry *realdentry; |
39b681f8 | 569 | struct inode *realinode; |
e9be9d5e | 570 | |
3d3c6b89 | 571 | realdentry = upperdentry ? upperdentry : stack[0].dentry; |
39b681f8 | 572 | realinode = d_inode(realdentry); |
3d3c6b89 | 573 | |
e9be9d5e | 574 | err = -ENOMEM; |
39b681f8 | 575 | inode = ovl_new_inode(dentry->d_sb, realinode->i_mode); |
e9be9d5e | 576 | if (!inode) |
3d3c6b89 | 577 | goto out_free_oe; |
39b681f8 | 578 | ovl_inode_init(inode, realinode, !!upperdentry); |
e9be9d5e MS |
579 | ovl_copyattr(realdentry->d_inode, inode); |
580 | } | |
581 | ||
3d3c6b89 | 582 | oe->opaque = upperopaque; |
e9be9d5e | 583 | oe->__upperdentry = upperdentry; |
3d3c6b89 MS |
584 | memcpy(oe->lowerstack, stack, sizeof(struct path) * ctr); |
585 | kfree(stack); | |
e9be9d5e MS |
586 | dentry->d_fsdata = oe; |
587 | d_add(dentry, inode); | |
588 | ||
589 | return NULL; | |
590 | ||
3d3c6b89 | 591 | out_free_oe: |
e9be9d5e | 592 | kfree(oe); |
3d3c6b89 MS |
593 | out_put: |
594 | for (i = 0; i < ctr; i++) | |
595 | dput(stack[i].dentry); | |
596 | kfree(stack); | |
597 | out_put_upper: | |
598 | dput(upperdentry); | |
e9be9d5e MS |
599 | out: |
600 | return ERR_PTR(err); | |
601 | } | |
602 | ||
603 | struct file *ovl_path_open(struct path *path, int flags) | |
604 | { | |
d719e8f2 | 605 | return dentry_open(path, flags | O_NOATIME, current_cred()); |
e9be9d5e MS |
606 | } |
607 | ||
608 | static void ovl_put_super(struct super_block *sb) | |
609 | { | |
610 | struct ovl_fs *ufs = sb->s_fs_info; | |
dd662667 | 611 | unsigned i; |
e9be9d5e MS |
612 | |
613 | dput(ufs->workdir); | |
614 | mntput(ufs->upper_mnt); | |
dd662667 MS |
615 | for (i = 0; i < ufs->numlower; i++) |
616 | mntput(ufs->lower_mnt[i]); | |
5ffdbe8b | 617 | kfree(ufs->lower_mnt); |
e9be9d5e | 618 | |
f45827e8 EZ |
619 | kfree(ufs->config.lowerdir); |
620 | kfree(ufs->config.upperdir); | |
621 | kfree(ufs->config.workdir); | |
3fe6e52f | 622 | put_cred(ufs->creator_cred); |
e9be9d5e MS |
623 | kfree(ufs); |
624 | } | |
625 | ||
cc259639 AW |
626 | /** |
627 | * ovl_statfs | |
628 | * @sb: The overlayfs super block | |
629 | * @buf: The struct kstatfs to fill in with stats | |
630 | * | |
631 | * Get the filesystem statistics. As writes always target the upper layer | |
4ebc5818 | 632 | * filesystem pass the statfs to the upper filesystem (if it exists) |
cc259639 AW |
633 | */ |
634 | static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf) | |
635 | { | |
636 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; | |
637 | struct dentry *root_dentry = dentry->d_sb->s_root; | |
638 | struct path path; | |
639 | int err; | |
640 | ||
4ebc5818 | 641 | ovl_path_real(root_dentry, &path); |
cc259639 AW |
642 | |
643 | err = vfs_statfs(&path, buf); | |
644 | if (!err) { | |
645 | buf->f_namelen = max(buf->f_namelen, ofs->lower_namelen); | |
646 | buf->f_type = OVERLAYFS_SUPER_MAGIC; | |
647 | } | |
648 | ||
649 | return err; | |
650 | } | |
651 | ||
f45827e8 EZ |
652 | /** |
653 | * ovl_show_options | |
654 | * | |
655 | * Prints the mount options for a given superblock. | |
656 | * Returns zero; does not fail. | |
657 | */ | |
658 | static int ovl_show_options(struct seq_file *m, struct dentry *dentry) | |
659 | { | |
660 | struct super_block *sb = dentry->d_sb; | |
661 | struct ovl_fs *ufs = sb->s_fs_info; | |
662 | ||
a068acf2 | 663 | seq_show_option(m, "lowerdir", ufs->config.lowerdir); |
53a08cb9 | 664 | if (ufs->config.upperdir) { |
a068acf2 KC |
665 | seq_show_option(m, "upperdir", ufs->config.upperdir); |
666 | seq_show_option(m, "workdir", ufs->config.workdir); | |
53a08cb9 | 667 | } |
8d3095f4 MS |
668 | if (ufs->config.default_permissions) |
669 | seq_puts(m, ",default_permissions"); | |
f45827e8 EZ |
670 | return 0; |
671 | } | |
672 | ||
3cdf6fe9 SL |
673 | static int ovl_remount(struct super_block *sb, int *flags, char *data) |
674 | { | |
675 | struct ovl_fs *ufs = sb->s_fs_info; | |
676 | ||
cc6f67bc | 677 | if (!(*flags & MS_RDONLY) && (!ufs->upper_mnt || !ufs->workdir)) |
3cdf6fe9 SL |
678 | return -EROFS; |
679 | ||
680 | return 0; | |
681 | } | |
682 | ||
e9be9d5e MS |
683 | static const struct super_operations ovl_super_operations = { |
684 | .put_super = ovl_put_super, | |
cc259639 | 685 | .statfs = ovl_statfs, |
f45827e8 | 686 | .show_options = ovl_show_options, |
3cdf6fe9 | 687 | .remount_fs = ovl_remount, |
eead4f2d | 688 | .drop_inode = generic_delete_inode, |
e9be9d5e MS |
689 | }; |
690 | ||
691 | enum { | |
692 | OPT_LOWERDIR, | |
693 | OPT_UPPERDIR, | |
694 | OPT_WORKDIR, | |
8d3095f4 | 695 | OPT_DEFAULT_PERMISSIONS, |
e9be9d5e MS |
696 | OPT_ERR, |
697 | }; | |
698 | ||
699 | static const match_table_t ovl_tokens = { | |
700 | {OPT_LOWERDIR, "lowerdir=%s"}, | |
701 | {OPT_UPPERDIR, "upperdir=%s"}, | |
702 | {OPT_WORKDIR, "workdir=%s"}, | |
8d3095f4 | 703 | {OPT_DEFAULT_PERMISSIONS, "default_permissions"}, |
e9be9d5e MS |
704 | {OPT_ERR, NULL} |
705 | }; | |
706 | ||
91c77947 MS |
707 | static char *ovl_next_opt(char **s) |
708 | { | |
709 | char *sbegin = *s; | |
710 | char *p; | |
711 | ||
712 | if (sbegin == NULL) | |
713 | return NULL; | |
714 | ||
715 | for (p = sbegin; *p; p++) { | |
716 | if (*p == '\\') { | |
717 | p++; | |
718 | if (!*p) | |
719 | break; | |
720 | } else if (*p == ',') { | |
721 | *p = '\0'; | |
722 | *s = p + 1; | |
723 | return sbegin; | |
724 | } | |
725 | } | |
726 | *s = NULL; | |
727 | return sbegin; | |
728 | } | |
729 | ||
e9be9d5e MS |
730 | static int ovl_parse_opt(char *opt, struct ovl_config *config) |
731 | { | |
732 | char *p; | |
733 | ||
91c77947 | 734 | while ((p = ovl_next_opt(&opt)) != NULL) { |
e9be9d5e MS |
735 | int token; |
736 | substring_t args[MAX_OPT_ARGS]; | |
737 | ||
738 | if (!*p) | |
739 | continue; | |
740 | ||
741 | token = match_token(p, ovl_tokens, args); | |
742 | switch (token) { | |
743 | case OPT_UPPERDIR: | |
744 | kfree(config->upperdir); | |
745 | config->upperdir = match_strdup(&args[0]); | |
746 | if (!config->upperdir) | |
747 | return -ENOMEM; | |
748 | break; | |
749 | ||
750 | case OPT_LOWERDIR: | |
751 | kfree(config->lowerdir); | |
752 | config->lowerdir = match_strdup(&args[0]); | |
753 | if (!config->lowerdir) | |
754 | return -ENOMEM; | |
755 | break; | |
756 | ||
757 | case OPT_WORKDIR: | |
758 | kfree(config->workdir); | |
759 | config->workdir = match_strdup(&args[0]); | |
760 | if (!config->workdir) | |
761 | return -ENOMEM; | |
762 | break; | |
763 | ||
8d3095f4 MS |
764 | case OPT_DEFAULT_PERMISSIONS: |
765 | config->default_permissions = true; | |
766 | break; | |
767 | ||
e9be9d5e | 768 | default: |
bead55ef | 769 | pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p); |
e9be9d5e MS |
770 | return -EINVAL; |
771 | } | |
772 | } | |
71cbad7e | 773 | |
774 | /* Workdir is useless in non-upper mount */ | |
775 | if (!config->upperdir && config->workdir) { | |
776 | pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n", | |
777 | config->workdir); | |
778 | kfree(config->workdir); | |
779 | config->workdir = NULL; | |
780 | } | |
781 | ||
e9be9d5e MS |
782 | return 0; |
783 | } | |
784 | ||
785 | #define OVL_WORKDIR_NAME "work" | |
786 | ||
787 | static struct dentry *ovl_workdir_create(struct vfsmount *mnt, | |
788 | struct dentry *dentry) | |
789 | { | |
790 | struct inode *dir = dentry->d_inode; | |
791 | struct dentry *work; | |
792 | int err; | |
793 | bool retried = false; | |
794 | ||
795 | err = mnt_want_write(mnt); | |
796 | if (err) | |
797 | return ERR_PTR(err); | |
798 | ||
5955102c | 799 | inode_lock_nested(dir, I_MUTEX_PARENT); |
e9be9d5e MS |
800 | retry: |
801 | work = lookup_one_len(OVL_WORKDIR_NAME, dentry, | |
802 | strlen(OVL_WORKDIR_NAME)); | |
803 | ||
804 | if (!IS_ERR(work)) { | |
805 | struct kstat stat = { | |
806 | .mode = S_IFDIR | 0, | |
807 | }; | |
808 | ||
809 | if (work->d_inode) { | |
810 | err = -EEXIST; | |
811 | if (retried) | |
812 | goto out_dput; | |
813 | ||
814 | retried = true; | |
815 | ovl_cleanup(dir, work); | |
816 | dput(work); | |
817 | goto retry; | |
818 | } | |
819 | ||
820 | err = ovl_create_real(dir, work, &stat, NULL, NULL, true); | |
821 | if (err) | |
822 | goto out_dput; | |
823 | } | |
824 | out_unlock: | |
5955102c | 825 | inode_unlock(dir); |
e9be9d5e MS |
826 | mnt_drop_write(mnt); |
827 | ||
828 | return work; | |
829 | ||
830 | out_dput: | |
831 | dput(work); | |
832 | work = ERR_PTR(err); | |
833 | goto out_unlock; | |
834 | } | |
835 | ||
91c77947 MS |
836 | static void ovl_unescape(char *s) |
837 | { | |
838 | char *d = s; | |
839 | ||
840 | for (;; s++, d++) { | |
841 | if (*s == '\\') | |
842 | s++; | |
843 | *d = *s; | |
844 | if (!*s) | |
845 | break; | |
846 | } | |
847 | } | |
848 | ||
ab508822 MS |
849 | static int ovl_mount_dir_noesc(const char *name, struct path *path) |
850 | { | |
a78d9f0d | 851 | int err = -EINVAL; |
ab508822 | 852 | |
a78d9f0d MS |
853 | if (!*name) { |
854 | pr_err("overlayfs: empty lowerdir\n"); | |
855 | goto out; | |
856 | } | |
ab508822 MS |
857 | err = kern_path(name, LOOKUP_FOLLOW, path); |
858 | if (err) { | |
859 | pr_err("overlayfs: failed to resolve '%s': %i\n", name, err); | |
860 | goto out; | |
861 | } | |
862 | err = -EINVAL; | |
7c03b5d4 | 863 | if (ovl_dentry_weird(path->dentry)) { |
ab508822 MS |
864 | pr_err("overlayfs: filesystem on '%s' not supported\n", name); |
865 | goto out_put; | |
866 | } | |
867 | if (!S_ISDIR(path->dentry->d_inode->i_mode)) { | |
868 | pr_err("overlayfs: '%s' not a directory\n", name); | |
869 | goto out_put; | |
870 | } | |
871 | return 0; | |
872 | ||
873 | out_put: | |
874 | path_put(path); | |
875 | out: | |
876 | return err; | |
877 | } | |
878 | ||
879 | static int ovl_mount_dir(const char *name, struct path *path) | |
880 | { | |
881 | int err = -ENOMEM; | |
882 | char *tmp = kstrdup(name, GFP_KERNEL); | |
883 | ||
884 | if (tmp) { | |
885 | ovl_unescape(tmp); | |
886 | err = ovl_mount_dir_noesc(tmp, path); | |
7c03b5d4 MS |
887 | |
888 | if (!err) | |
889 | if (ovl_dentry_remote(path->dentry)) { | |
890 | pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n", | |
891 | tmp); | |
892 | path_put(path); | |
893 | err = -EINVAL; | |
894 | } | |
ab508822 MS |
895 | kfree(tmp); |
896 | } | |
897 | return err; | |
898 | } | |
899 | ||
900 | static int ovl_lower_dir(const char *name, struct path *path, long *namelen, | |
7c03b5d4 | 901 | int *stack_depth, bool *remote) |
ab508822 MS |
902 | { |
903 | int err; | |
904 | struct kstatfs statfs; | |
905 | ||
a78d9f0d | 906 | err = ovl_mount_dir_noesc(name, path); |
ab508822 MS |
907 | if (err) |
908 | goto out; | |
909 | ||
910 | err = vfs_statfs(path, &statfs); | |
911 | if (err) { | |
912 | pr_err("overlayfs: statfs failed on '%s'\n", name); | |
913 | goto out_put; | |
914 | } | |
915 | *namelen = max(*namelen, statfs.f_namelen); | |
916 | *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth); | |
917 | ||
7c03b5d4 MS |
918 | if (ovl_dentry_remote(path->dentry)) |
919 | *remote = true; | |
920 | ||
ab508822 MS |
921 | return 0; |
922 | ||
923 | out_put: | |
924 | path_put(path); | |
925 | out: | |
926 | return err; | |
927 | } | |
928 | ||
e9be9d5e MS |
929 | /* Workdir should not be subdir of upperdir and vice versa */ |
930 | static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir) | |
931 | { | |
932 | bool ok = false; | |
933 | ||
934 | if (workdir != upperdir) { | |
935 | ok = (lock_rename(workdir, upperdir) == NULL); | |
936 | unlock_rename(workdir, upperdir); | |
937 | } | |
938 | return ok; | |
939 | } | |
940 | ||
a78d9f0d MS |
941 | static unsigned int ovl_split_lowerdirs(char *str) |
942 | { | |
943 | unsigned int ctr = 1; | |
944 | char *s, *d; | |
945 | ||
946 | for (s = d = str;; s++, d++) { | |
947 | if (*s == '\\') { | |
948 | s++; | |
949 | } else if (*s == ':') { | |
950 | *d = '\0'; | |
951 | ctr++; | |
952 | continue; | |
953 | } | |
954 | *d = *s; | |
955 | if (!*s) | |
956 | break; | |
957 | } | |
958 | return ctr; | |
959 | } | |
960 | ||
e9be9d5e MS |
961 | static int ovl_fill_super(struct super_block *sb, void *data, int silent) |
962 | { | |
53a08cb9 MS |
963 | struct path upperpath = { NULL, NULL }; |
964 | struct path workpath = { NULL, NULL }; | |
e9be9d5e | 965 | struct dentry *root_dentry; |
39b681f8 | 966 | struct inode *realinode; |
e9be9d5e MS |
967 | struct ovl_entry *oe; |
968 | struct ovl_fs *ufs; | |
a78d9f0d MS |
969 | struct path *stack = NULL; |
970 | char *lowertmp; | |
971 | char *lower; | |
972 | unsigned int numlower; | |
973 | unsigned int stacklen = 0; | |
dd662667 | 974 | unsigned int i; |
7c03b5d4 | 975 | bool remote = false; |
e9be9d5e MS |
976 | int err; |
977 | ||
f45827e8 EZ |
978 | err = -ENOMEM; |
979 | ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL); | |
980 | if (!ufs) | |
e9be9d5e MS |
981 | goto out; |
982 | ||
f45827e8 EZ |
983 | err = ovl_parse_opt((char *) data, &ufs->config); |
984 | if (err) | |
985 | goto out_free_config; | |
986 | ||
e9be9d5e | 987 | err = -EINVAL; |
53a08cb9 | 988 | if (!ufs->config.lowerdir) { |
07f2af7b KK |
989 | if (!silent) |
990 | pr_err("overlayfs: missing 'lowerdir'\n"); | |
e9be9d5e MS |
991 | goto out_free_config; |
992 | } | |
993 | ||
53a08cb9 | 994 | sb->s_stack_depth = 0; |
cf9a6784 | 995 | sb->s_maxbytes = MAX_LFS_FILESIZE; |
53a08cb9 | 996 | if (ufs->config.upperdir) { |
53a08cb9 MS |
997 | if (!ufs->config.workdir) { |
998 | pr_err("overlayfs: missing 'workdir'\n"); | |
999 | goto out_free_config; | |
1000 | } | |
e9be9d5e | 1001 | |
53a08cb9 MS |
1002 | err = ovl_mount_dir(ufs->config.upperdir, &upperpath); |
1003 | if (err) | |
1004 | goto out_free_config; | |
e9be9d5e | 1005 | |
71cbad7e | 1006 | /* Upper fs should not be r/o */ |
1007 | if (upperpath.mnt->mnt_sb->s_flags & MS_RDONLY) { | |
1008 | pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n"); | |
1009 | err = -EINVAL; | |
1010 | goto out_put_upperpath; | |
1011 | } | |
1012 | ||
53a08cb9 MS |
1013 | err = ovl_mount_dir(ufs->config.workdir, &workpath); |
1014 | if (err) | |
1015 | goto out_put_upperpath; | |
1016 | ||
2f83fd8c | 1017 | err = -EINVAL; |
53a08cb9 MS |
1018 | if (upperpath.mnt != workpath.mnt) { |
1019 | pr_err("overlayfs: workdir and upperdir must reside under the same mount\n"); | |
1020 | goto out_put_workpath; | |
1021 | } | |
1022 | if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) { | |
1023 | pr_err("overlayfs: workdir and upperdir must be separate subtrees\n"); | |
1024 | goto out_put_workpath; | |
1025 | } | |
1026 | sb->s_stack_depth = upperpath.mnt->mnt_sb->s_stack_depth; | |
cc259639 | 1027 | } |
a78d9f0d MS |
1028 | err = -ENOMEM; |
1029 | lowertmp = kstrdup(ufs->config.lowerdir, GFP_KERNEL); | |
1030 | if (!lowertmp) | |
ab508822 | 1031 | goto out_put_workpath; |
69c433ed | 1032 | |
a78d9f0d MS |
1033 | err = -EINVAL; |
1034 | stacklen = ovl_split_lowerdirs(lowertmp); | |
6be4506e | 1035 | if (stacklen > OVL_MAX_STACK) { |
1036 | pr_err("overlayfs: too many lower directries, limit is %d\n", | |
1037 | OVL_MAX_STACK); | |
a78d9f0d | 1038 | goto out_free_lowertmp; |
6be4506e | 1039 | } else if (!ufs->config.upperdir && stacklen == 1) { |
1040 | pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n"); | |
1041 | goto out_free_lowertmp; | |
1042 | } | |
a78d9f0d MS |
1043 | |
1044 | stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL); | |
1045 | if (!stack) | |
1046 | goto out_free_lowertmp; | |
1047 | ||
1048 | lower = lowertmp; | |
1049 | for (numlower = 0; numlower < stacklen; numlower++) { | |
1050 | err = ovl_lower_dir(lower, &stack[numlower], | |
7c03b5d4 MS |
1051 | &ufs->lower_namelen, &sb->s_stack_depth, |
1052 | &remote); | |
a78d9f0d MS |
1053 | if (err) |
1054 | goto out_put_lowerpath; | |
1055 | ||
1056 | lower = strchr(lower, '\0') + 1; | |
1057 | } | |
1058 | ||
69c433ed | 1059 | err = -EINVAL; |
ab508822 | 1060 | sb->s_stack_depth++; |
69c433ed MS |
1061 | if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) { |
1062 | pr_err("overlayfs: maximum fs stacking depth exceeded\n"); | |
3b7a9a24 | 1063 | goto out_put_lowerpath; |
69c433ed MS |
1064 | } |
1065 | ||
53a08cb9 MS |
1066 | if (ufs->config.upperdir) { |
1067 | ufs->upper_mnt = clone_private_mount(&upperpath); | |
1068 | err = PTR_ERR(ufs->upper_mnt); | |
1069 | if (IS_ERR(ufs->upper_mnt)) { | |
1070 | pr_err("overlayfs: failed to clone upperpath\n"); | |
1071 | goto out_put_lowerpath; | |
1072 | } | |
d719e8f2 MS |
1073 | /* Don't inherit atime flags */ |
1074 | ufs->upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME); | |
1075 | ||
1076 | sb->s_time_gran = ufs->upper_mnt->mnt_sb->s_time_gran; | |
3b7a9a24 | 1077 | |
53a08cb9 MS |
1078 | ufs->workdir = ovl_workdir_create(ufs->upper_mnt, workpath.dentry); |
1079 | err = PTR_ERR(ufs->workdir); | |
1080 | if (IS_ERR(ufs->workdir)) { | |
cc6f67bc MS |
1081 | pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n", |
1082 | ufs->config.workdir, OVL_WORKDIR_NAME, -err); | |
1083 | sb->s_flags |= MS_RDONLY; | |
1084 | ufs->workdir = NULL; | |
53a08cb9 | 1085 | } |
45aebeaf VG |
1086 | |
1087 | /* | |
1088 | * Upper should support d_type, else whiteouts are visible. | |
1089 | * Given workdir and upper are on same fs, we can do | |
21765194 VG |
1090 | * iterate_dir() on workdir. This check requires successful |
1091 | * creation of workdir in previous step. | |
45aebeaf | 1092 | */ |
21765194 VG |
1093 | if (ufs->workdir) { |
1094 | err = ovl_check_d_type_supported(&workpath); | |
1095 | if (err < 0) | |
1096 | goto out_put_workdir; | |
45aebeaf | 1097 | |
e7c0b599 VG |
1098 | /* |
1099 | * We allowed this configuration and don't want to | |
1100 | * break users over kernel upgrade. So warn instead | |
1101 | * of erroring out. | |
1102 | */ | |
1103 | if (!err) | |
1104 | pr_warn("overlayfs: upper fs needs to support d_type.\n"); | |
45aebeaf | 1105 | } |
e9be9d5e MS |
1106 | } |
1107 | ||
2f83fd8c | 1108 | err = -ENOMEM; |
a78d9f0d | 1109 | ufs->lower_mnt = kcalloc(numlower, sizeof(struct vfsmount *), GFP_KERNEL); |
dd662667 | 1110 | if (ufs->lower_mnt == NULL) |
3b7a9a24 | 1111 | goto out_put_workdir; |
a78d9f0d MS |
1112 | for (i = 0; i < numlower; i++) { |
1113 | struct vfsmount *mnt = clone_private_mount(&stack[i]); | |
dd662667 | 1114 | |
2f83fd8c | 1115 | err = PTR_ERR(mnt); |
a78d9f0d MS |
1116 | if (IS_ERR(mnt)) { |
1117 | pr_err("overlayfs: failed to clone lowerpath\n"); | |
1118 | goto out_put_lower_mnt; | |
1119 | } | |
1120 | /* | |
1121 | * Make lower_mnt R/O. That way fchmod/fchown on lower file | |
1122 | * will fail instead of modifying lower fs. | |
1123 | */ | |
d719e8f2 | 1124 | mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME; |
dd662667 | 1125 | |
a78d9f0d MS |
1126 | ufs->lower_mnt[ufs->numlower] = mnt; |
1127 | ufs->numlower++; | |
1128 | } | |
e9be9d5e | 1129 | |
71cbad7e | 1130 | /* If the upper fs is nonexistent, we mark overlayfs r/o too */ |
1131 | if (!ufs->upper_mnt) | |
e9be9d5e MS |
1132 | sb->s_flags |= MS_RDONLY; |
1133 | ||
7c03b5d4 MS |
1134 | if (remote) |
1135 | sb->s_d_op = &ovl_reval_dentry_operations; | |
1136 | else | |
1137 | sb->s_d_op = &ovl_dentry_operations; | |
e9be9d5e | 1138 | |
3fe6e52f AM |
1139 | ufs->creator_cred = prepare_creds(); |
1140 | if (!ufs->creator_cred) | |
1141 | goto out_put_lower_mnt; | |
1142 | ||
e9be9d5e | 1143 | err = -ENOMEM; |
a78d9f0d | 1144 | oe = ovl_alloc_entry(numlower); |
3b7a9a24 | 1145 | if (!oe) |
3fe6e52f | 1146 | goto out_put_cred; |
e9be9d5e | 1147 | |
39b681f8 | 1148 | root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR)); |
e9be9d5e | 1149 | if (!root_dentry) |
3b7a9a24 | 1150 | goto out_free_oe; |
e9be9d5e MS |
1151 | |
1152 | mntput(upperpath.mnt); | |
a78d9f0d MS |
1153 | for (i = 0; i < numlower; i++) |
1154 | mntput(stack[i].mnt); | |
e9be9d5e | 1155 | path_put(&workpath); |
a78d9f0d | 1156 | kfree(lowertmp); |
e9be9d5e MS |
1157 | |
1158 | oe->__upperdentry = upperpath.dentry; | |
a78d9f0d MS |
1159 | for (i = 0; i < numlower; i++) { |
1160 | oe->lowerstack[i].dentry = stack[i].dentry; | |
1161 | oe->lowerstack[i].mnt = ufs->lower_mnt[i]; | |
1162 | } | |
0f95502a | 1163 | kfree(stack); |
e9be9d5e MS |
1164 | |
1165 | root_dentry->d_fsdata = oe; | |
1166 | ||
39b681f8 MS |
1167 | realinode = d_inode(ovl_dentry_real(root_dentry)); |
1168 | ovl_inode_init(d_inode(root_dentry), realinode, !!upperpath.dentry); | |
1169 | ovl_copyattr(realinode, d_inode(root_dentry)); | |
ed06e069 | 1170 | |
cc259639 | 1171 | sb->s_magic = OVERLAYFS_SUPER_MAGIC; |
e9be9d5e MS |
1172 | sb->s_op = &ovl_super_operations; |
1173 | sb->s_root = root_dentry; | |
1174 | sb->s_fs_info = ufs; | |
39a25b2b | 1175 | sb->s_flags |= MS_POSIXACL; |
e9be9d5e MS |
1176 | |
1177 | return 0; | |
1178 | ||
3b7a9a24 MS |
1179 | out_free_oe: |
1180 | kfree(oe); | |
3fe6e52f AM |
1181 | out_put_cred: |
1182 | put_cred(ufs->creator_cred); | |
e9be9d5e | 1183 | out_put_lower_mnt: |
dd662667 MS |
1184 | for (i = 0; i < ufs->numlower; i++) |
1185 | mntput(ufs->lower_mnt[i]); | |
1186 | kfree(ufs->lower_mnt); | |
3b7a9a24 MS |
1187 | out_put_workdir: |
1188 | dput(ufs->workdir); | |
e9be9d5e | 1189 | mntput(ufs->upper_mnt); |
e9be9d5e | 1190 | out_put_lowerpath: |
a78d9f0d MS |
1191 | for (i = 0; i < numlower; i++) |
1192 | path_put(&stack[i]); | |
1193 | kfree(stack); | |
1194 | out_free_lowertmp: | |
1195 | kfree(lowertmp); | |
3b7a9a24 MS |
1196 | out_put_workpath: |
1197 | path_put(&workpath); | |
e9be9d5e MS |
1198 | out_put_upperpath: |
1199 | path_put(&upperpath); | |
e9be9d5e | 1200 | out_free_config: |
f45827e8 EZ |
1201 | kfree(ufs->config.lowerdir); |
1202 | kfree(ufs->config.upperdir); | |
1203 | kfree(ufs->config.workdir); | |
1204 | kfree(ufs); | |
e9be9d5e MS |
1205 | out: |
1206 | return err; | |
1207 | } | |
1208 | ||
1209 | static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags, | |
1210 | const char *dev_name, void *raw_data) | |
1211 | { | |
1212 | return mount_nodev(fs_type, flags, raw_data, ovl_fill_super); | |
1213 | } | |
1214 | ||
1215 | static struct file_system_type ovl_fs_type = { | |
1216 | .owner = THIS_MODULE, | |
ef94b186 | 1217 | .name = "overlay", |
e9be9d5e MS |
1218 | .mount = ovl_mount, |
1219 | .kill_sb = kill_anon_super, | |
1220 | }; | |
ef94b186 | 1221 | MODULE_ALIAS_FS("overlay"); |
e9be9d5e MS |
1222 | |
1223 | static int __init ovl_init(void) | |
1224 | { | |
1225 | return register_filesystem(&ovl_fs_type); | |
1226 | } | |
1227 | ||
1228 | static void __exit ovl_exit(void) | |
1229 | { | |
1230 | unregister_filesystem(&ovl_fs_type); | |
1231 | } | |
1232 | ||
1233 | module_init(ovl_init); | |
1234 | module_exit(ovl_exit); |