]>
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> | |
19 | #include <linux/sched.h> | |
cc259639 | 20 | #include <linux/statfs.h> |
f45827e8 | 21 | #include <linux/seq_file.h> |
d837a49b | 22 | #include <linux/posix_acl_xattr.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 | |
38e813db | 83 | * location. |
45d11738 KK |
84 | */ |
85 | if (oe->numlower && S_ISDIR(dentry->d_inode->i_mode)) | |
86 | type |= __OVL_PATH_MERGE; | |
9d7459d8 MS |
87 | } else { |
88 | if (oe->numlower > 1) | |
89 | type |= __OVL_PATH_MERGE; | |
e9be9d5e | 90 | } |
1afaba1e | 91 | return type; |
e9be9d5e MS |
92 | } |
93 | ||
94 | static struct dentry *ovl_upperdentry_dereference(struct ovl_entry *oe) | |
95 | { | |
71d50928 | 96 | return lockless_dereference(oe->__upperdentry); |
e9be9d5e MS |
97 | } |
98 | ||
99 | void ovl_path_upper(struct dentry *dentry, struct path *path) | |
100 | { | |
101 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; | |
102 | struct ovl_entry *oe = dentry->d_fsdata; | |
103 | ||
104 | path->mnt = ofs->upper_mnt; | |
105 | path->dentry = ovl_upperdentry_dereference(oe); | |
106 | } | |
107 | ||
108 | enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path) | |
109 | { | |
e9be9d5e MS |
110 | enum ovl_path_type type = ovl_path_type(dentry); |
111 | ||
1afaba1e | 112 | if (!OVL_TYPE_UPPER(type)) |
e9be9d5e MS |
113 | ovl_path_lower(dentry, path); |
114 | else | |
115 | ovl_path_upper(dentry, path); | |
116 | ||
117 | return type; | |
118 | } | |
119 | ||
120 | struct dentry *ovl_dentry_upper(struct dentry *dentry) | |
121 | { | |
122 | struct ovl_entry *oe = dentry->d_fsdata; | |
123 | ||
124 | return ovl_upperdentry_dereference(oe); | |
125 | } | |
126 | ||
127 | struct dentry *ovl_dentry_lower(struct dentry *dentry) | |
128 | { | |
129 | struct ovl_entry *oe = dentry->d_fsdata; | |
130 | ||
dd662667 | 131 | return __ovl_dentry_lower(oe); |
e9be9d5e MS |
132 | } |
133 | ||
134 | struct dentry *ovl_dentry_real(struct dentry *dentry) | |
135 | { | |
136 | struct ovl_entry *oe = dentry->d_fsdata; | |
137 | struct dentry *realdentry; | |
138 | ||
139 | realdentry = ovl_upperdentry_dereference(oe); | |
140 | if (!realdentry) | |
dd662667 | 141 | realdentry = __ovl_dentry_lower(oe); |
e9be9d5e MS |
142 | |
143 | return realdentry; | |
144 | } | |
145 | ||
39b681f8 MS |
146 | static void ovl_inode_init(struct inode *inode, struct inode *realinode, |
147 | bool is_upper) | |
e9be9d5e | 148 | { |
39b681f8 MS |
149 | WRITE_ONCE(inode->i_private, (unsigned long) realinode | |
150 | (is_upper ? OVL_ISUPPER_MASK : 0)); | |
39a25b2b VG |
151 | } |
152 | ||
8d3095f4 MS |
153 | struct vfsmount *ovl_entry_mnt_real(struct ovl_entry *oe, struct inode *inode, |
154 | bool is_upper) | |
155 | { | |
156 | if (is_upper) { | |
157 | struct ovl_fs *ofs = inode->i_sb->s_fs_info; | |
158 | ||
159 | return ofs->upper_mnt; | |
160 | } else { | |
161 | return oe->numlower ? oe->lowerstack[0].mnt : NULL; | |
162 | } | |
163 | } | |
164 | ||
e9be9d5e MS |
165 | struct ovl_dir_cache *ovl_dir_cache(struct dentry *dentry) |
166 | { | |
167 | struct ovl_entry *oe = dentry->d_fsdata; | |
168 | ||
169 | return oe->cache; | |
170 | } | |
171 | ||
172 | void ovl_set_dir_cache(struct dentry *dentry, struct ovl_dir_cache *cache) | |
173 | { | |
174 | struct ovl_entry *oe = dentry->d_fsdata; | |
175 | ||
176 | oe->cache = cache; | |
177 | } | |
178 | ||
179 | void ovl_path_lower(struct dentry *dentry, struct path *path) | |
180 | { | |
e9be9d5e MS |
181 | struct ovl_entry *oe = dentry->d_fsdata; |
182 | ||
dd662667 | 183 | *path = oe->numlower ? oe->lowerstack[0] : (struct path) { NULL, NULL }; |
e9be9d5e MS |
184 | } |
185 | ||
186 | int ovl_want_write(struct dentry *dentry) | |
187 | { | |
188 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; | |
189 | return mnt_want_write(ofs->upper_mnt); | |
190 | } | |
191 | ||
192 | void ovl_drop_write(struct dentry *dentry) | |
193 | { | |
194 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; | |
195 | mnt_drop_write(ofs->upper_mnt); | |
196 | } | |
197 | ||
198 | struct dentry *ovl_workdir(struct dentry *dentry) | |
199 | { | |
200 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; | |
201 | return ofs->workdir; | |
202 | } | |
203 | ||
204 | bool ovl_dentry_is_opaque(struct dentry *dentry) | |
205 | { | |
206 | struct ovl_entry *oe = dentry->d_fsdata; | |
207 | return oe->opaque; | |
208 | } | |
209 | ||
c412ce49 MS |
210 | bool ovl_dentry_is_whiteout(struct dentry *dentry) |
211 | { | |
212 | return !dentry->d_inode && ovl_dentry_is_opaque(dentry); | |
213 | } | |
214 | ||
e9be9d5e MS |
215 | void ovl_dentry_set_opaque(struct dentry *dentry, bool opaque) |
216 | { | |
217 | struct ovl_entry *oe = dentry->d_fsdata; | |
218 | oe->opaque = opaque; | |
219 | } | |
220 | ||
221 | void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry) | |
222 | { | |
223 | struct ovl_entry *oe = dentry->d_fsdata; | |
224 | ||
5955102c | 225 | WARN_ON(!inode_is_locked(upperdentry->d_parent->d_inode)); |
e9be9d5e | 226 | WARN_ON(oe->__upperdentry); |
e9be9d5e MS |
227 | /* |
228 | * Make sure upperdentry is consistent before making it visible to | |
229 | * ovl_upperdentry_dereference(). | |
230 | */ | |
231 | smp_wmb(); | |
232 | oe->__upperdentry = upperdentry; | |
233 | } | |
234 | ||
39b681f8 MS |
235 | void ovl_inode_update(struct inode *inode, struct inode *upperinode) |
236 | { | |
237 | WARN_ON(!upperinode); | |
51f7e52d | 238 | WARN_ON(!inode_unhashed(inode)); |
39b681f8 MS |
239 | WRITE_ONCE(inode->i_private, |
240 | (unsigned long) upperinode | OVL_ISUPPER_MASK); | |
51f7e52d MS |
241 | if (!S_ISDIR(upperinode->i_mode)) |
242 | __insert_inode_hash(inode, (unsigned long) upperinode); | |
39b681f8 MS |
243 | } |
244 | ||
e9be9d5e MS |
245 | void ovl_dentry_version_inc(struct dentry *dentry) |
246 | { | |
247 | struct ovl_entry *oe = dentry->d_fsdata; | |
248 | ||
5955102c | 249 | WARN_ON(!inode_is_locked(dentry->d_inode)); |
e9be9d5e MS |
250 | oe->version++; |
251 | } | |
252 | ||
253 | u64 ovl_dentry_version_get(struct dentry *dentry) | |
254 | { | |
255 | struct ovl_entry *oe = dentry->d_fsdata; | |
256 | ||
5955102c | 257 | WARN_ON(!inode_is_locked(dentry->d_inode)); |
e9be9d5e MS |
258 | return oe->version; |
259 | } | |
260 | ||
261 | bool ovl_is_whiteout(struct dentry *dentry) | |
262 | { | |
263 | struct inode *inode = dentry->d_inode; | |
264 | ||
265 | return inode && IS_WHITEOUT(inode); | |
266 | } | |
267 | ||
3fe6e52f AM |
268 | const struct cred *ovl_override_creds(struct super_block *sb) |
269 | { | |
270 | struct ovl_fs *ofs = sb->s_fs_info; | |
271 | ||
272 | return override_creds(ofs->creator_cred); | |
273 | } | |
274 | ||
e9be9d5e MS |
275 | static bool ovl_is_opaquedir(struct dentry *dentry) |
276 | { | |
277 | int res; | |
278 | char val; | |
e9be9d5e | 279 | |
2b6bc7f4 | 280 | if (!d_is_dir(dentry)) |
e9be9d5e MS |
281 | return false; |
282 | ||
2b6bc7f4 | 283 | res = vfs_getxattr(dentry, OVL_XATTR_OPAQUE, &val, 1); |
e9be9d5e MS |
284 | if (res == 1 && val == 'y') |
285 | return true; | |
286 | ||
287 | return false; | |
288 | } | |
289 | ||
290 | static void ovl_dentry_release(struct dentry *dentry) | |
291 | { | |
292 | struct ovl_entry *oe = dentry->d_fsdata; | |
293 | ||
294 | if (oe) { | |
dd662667 MS |
295 | unsigned int i; |
296 | ||
e9be9d5e | 297 | dput(oe->__upperdentry); |
dd662667 MS |
298 | for (i = 0; i < oe->numlower; i++) |
299 | dput(oe->lowerstack[i].dentry); | |
e9be9d5e MS |
300 | kfree_rcu(oe, rcu); |
301 | } | |
302 | } | |
303 | ||
2d902671 MS |
304 | static struct dentry *ovl_d_real(struct dentry *dentry, |
305 | const struct inode *inode, | |
306 | unsigned int open_flags) | |
d101a125 MS |
307 | { |
308 | struct dentry *real; | |
309 | ||
ca4c8a3a | 310 | if (!d_is_reg(dentry)) { |
d101a125 MS |
311 | if (!inode || inode == d_inode(dentry)) |
312 | return dentry; | |
313 | goto bug; | |
314 | } | |
315 | ||
2d902671 MS |
316 | if (d_is_negative(dentry)) |
317 | return dentry; | |
318 | ||
319 | if (open_flags) { | |
320 | int err = ovl_open_maybe_copy_up(dentry, open_flags); | |
321 | ||
322 | if (err) | |
323 | return ERR_PTR(err); | |
324 | } | |
325 | ||
d101a125 MS |
326 | real = ovl_dentry_upper(dentry); |
327 | if (real && (!inode || inode == d_inode(real))) | |
328 | return real; | |
329 | ||
330 | real = ovl_dentry_lower(dentry); | |
331 | if (!real) | |
332 | goto bug; | |
333 | ||
c4fcfc16 MS |
334 | /* Handle recursion */ |
335 | real = d_real(real, inode, open_flags); | |
336 | ||
d101a125 MS |
337 | if (!inode || inode == d_inode(real)) |
338 | return real; | |
d101a125 | 339 | bug: |
656189d2 | 340 | WARN(1, "ovl_d_real(%pd4, %s:%lu): real dentry not found\n", dentry, |
d101a125 MS |
341 | inode ? inode->i_sb->s_id : "NULL", inode ? inode->i_ino : 0); |
342 | return dentry; | |
343 | } | |
344 | ||
7c03b5d4 MS |
345 | static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags) |
346 | { | |
347 | struct ovl_entry *oe = dentry->d_fsdata; | |
348 | unsigned int i; | |
349 | int ret = 1; | |
350 | ||
351 | for (i = 0; i < oe->numlower; i++) { | |
352 | struct dentry *d = oe->lowerstack[i].dentry; | |
353 | ||
354 | if (d->d_flags & DCACHE_OP_REVALIDATE) { | |
355 | ret = d->d_op->d_revalidate(d, flags); | |
356 | if (ret < 0) | |
357 | return ret; | |
358 | if (!ret) { | |
359 | if (!(flags & LOOKUP_RCU)) | |
360 | d_invalidate(d); | |
361 | return -ESTALE; | |
362 | } | |
363 | } | |
364 | } | |
365 | return 1; | |
366 | } | |
367 | ||
368 | static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags) | |
369 | { | |
370 | struct ovl_entry *oe = dentry->d_fsdata; | |
371 | unsigned int i; | |
372 | int ret = 1; | |
373 | ||
374 | for (i = 0; i < oe->numlower; i++) { | |
375 | struct dentry *d = oe->lowerstack[i].dentry; | |
376 | ||
377 | if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE) { | |
378 | ret = d->d_op->d_weak_revalidate(d, flags); | |
379 | if (ret <= 0) | |
380 | break; | |
381 | } | |
382 | } | |
383 | return ret; | |
384 | } | |
385 | ||
e9be9d5e MS |
386 | static const struct dentry_operations ovl_dentry_operations = { |
387 | .d_release = ovl_dentry_release, | |
d101a125 | 388 | .d_real = ovl_d_real, |
e9be9d5e MS |
389 | }; |
390 | ||
7c03b5d4 MS |
391 | static const struct dentry_operations ovl_reval_dentry_operations = { |
392 | .d_release = ovl_dentry_release, | |
d101a125 | 393 | .d_real = ovl_d_real, |
7c03b5d4 MS |
394 | .d_revalidate = ovl_dentry_revalidate, |
395 | .d_weak_revalidate = ovl_dentry_weak_revalidate, | |
396 | }; | |
397 | ||
dd662667 | 398 | static struct ovl_entry *ovl_alloc_entry(unsigned int numlower) |
e9be9d5e | 399 | { |
dd662667 MS |
400 | size_t size = offsetof(struct ovl_entry, lowerstack[numlower]); |
401 | struct ovl_entry *oe = kzalloc(size, GFP_KERNEL); | |
402 | ||
403 | if (oe) | |
404 | oe->numlower = numlower; | |
405 | ||
406 | return oe; | |
e9be9d5e MS |
407 | } |
408 | ||
7c03b5d4 MS |
409 | static bool ovl_dentry_remote(struct dentry *dentry) |
410 | { | |
411 | return dentry->d_flags & | |
76bc8e28 MS |
412 | (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE | |
413 | DCACHE_OP_REAL); | |
7c03b5d4 MS |
414 | } |
415 | ||
416 | static bool ovl_dentry_weird(struct dentry *dentry) | |
417 | { | |
418 | return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT | | |
419 | DCACHE_MANAGE_TRANSIT | | |
420 | DCACHE_OP_HASH | | |
421 | DCACHE_OP_COMPARE); | |
422 | } | |
423 | ||
2b6bc7f4 | 424 | static inline struct dentry *ovl_lookup_real(struct dentry *dir, |
29c42e80 | 425 | const struct qstr *name) |
e9be9d5e MS |
426 | { |
427 | struct dentry *dentry; | |
428 | ||
c1b2cc1a | 429 | dentry = lookup_one_len_unlocked(name->name, dir, name->len); |
e9be9d5e MS |
430 | if (IS_ERR(dentry)) { |
431 | if (PTR_ERR(dentry) == -ENOENT) | |
432 | dentry = NULL; | |
433 | } else if (!dentry->d_inode) { | |
434 | dput(dentry); | |
435 | dentry = NULL; | |
7c03b5d4 | 436 | } else if (ovl_dentry_weird(dentry)) { |
a6f15d9a | 437 | dput(dentry); |
7c03b5d4 | 438 | /* Don't support traversing automounts and other weirdness */ |
a6f15d9a | 439 | dentry = ERR_PTR(-EREMOTE); |
e9be9d5e MS |
440 | } |
441 | return dentry; | |
442 | } | |
443 | ||
5ef88da5 MS |
444 | /* |
445 | * Returns next layer in stack starting from top. | |
446 | * Returns -1 if this is the last layer. | |
447 | */ | |
448 | int ovl_path_next(int idx, struct dentry *dentry, struct path *path) | |
449 | { | |
450 | struct ovl_entry *oe = dentry->d_fsdata; | |
451 | ||
452 | BUG_ON(idx < 0); | |
453 | if (idx == 0) { | |
454 | ovl_path_upper(dentry, path); | |
455 | if (path->dentry) | |
456 | return oe->numlower ? 1 : -1; | |
457 | idx++; | |
458 | } | |
459 | BUG_ON(idx > oe->numlower); | |
460 | *path = oe->lowerstack[idx - 1]; | |
461 | ||
462 | return (idx < oe->numlower) ? idx + 1 : -1; | |
463 | } | |
464 | ||
e9be9d5e MS |
465 | struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry, |
466 | unsigned int flags) | |
467 | { | |
468 | struct ovl_entry *oe; | |
2b6bc7f4 | 469 | const struct cred *old_cred; |
3d3c6b89 MS |
470 | struct ovl_entry *poe = dentry->d_parent->d_fsdata; |
471 | struct path *stack = NULL; | |
472 | struct dentry *upperdir, *upperdentry = NULL; | |
473 | unsigned int ctr = 0; | |
e9be9d5e | 474 | struct inode *inode = NULL; |
3d3c6b89 | 475 | bool upperopaque = false; |
8ee6059c MS |
476 | bool stop = false; |
477 | bool isdir = false; | |
478 | struct dentry *this; | |
3d3c6b89 | 479 | unsigned int i; |
e9be9d5e MS |
480 | int err; |
481 | ||
2b6bc7f4 | 482 | old_cred = ovl_override_creds(dentry->d_sb); |
3d3c6b89 | 483 | upperdir = ovl_upperdentry_dereference(poe); |
e9be9d5e | 484 | if (upperdir) { |
2b6bc7f4 | 485 | this = ovl_lookup_real(upperdir, &dentry->d_name); |
3d3c6b89 MS |
486 | err = PTR_ERR(this); |
487 | if (IS_ERR(this)) | |
488 | goto out; | |
489 | ||
3e01cee3 | 490 | if (this) { |
7c03b5d4 MS |
491 | if (unlikely(ovl_dentry_remote(this))) { |
492 | dput(this); | |
493 | err = -EREMOTE; | |
494 | goto out; | |
495 | } | |
3d3c6b89 MS |
496 | if (ovl_is_whiteout(this)) { |
497 | dput(this); | |
498 | this = NULL; | |
8ee6059c MS |
499 | stop = upperopaque = true; |
500 | } else if (!d_is_dir(this)) { | |
501 | stop = true; | |
502 | } else { | |
503 | isdir = true; | |
504 | if (poe->numlower && ovl_is_opaquedir(this)) | |
505 | stop = upperopaque = true; | |
e9be9d5e MS |
506 | } |
507 | } | |
8ee6059c | 508 | upperdentry = this; |
e9be9d5e | 509 | } |
3d3c6b89 | 510 | |
8ee6059c | 511 | if (!stop && poe->numlower) { |
3d3c6b89 MS |
512 | err = -ENOMEM; |
513 | stack = kcalloc(poe->numlower, sizeof(struct path), GFP_KERNEL); | |
514 | if (!stack) | |
515 | goto out_put_upper; | |
e9be9d5e MS |
516 | } |
517 | ||
8ee6059c | 518 | for (i = 0; !stop && i < poe->numlower; i++) { |
3d3c6b89 MS |
519 | struct path lowerpath = poe->lowerstack[i]; |
520 | ||
2b6bc7f4 | 521 | this = ovl_lookup_real(lowerpath.dentry, &dentry->d_name); |
3d3c6b89 | 522 | err = PTR_ERR(this); |
09e10322 MS |
523 | if (IS_ERR(this)) { |
524 | /* | |
525 | * If it's positive, then treat ENAMETOOLONG as ENOENT. | |
526 | */ | |
527 | if (err == -ENAMETOOLONG && (upperdentry || ctr)) | |
528 | continue; | |
3d3c6b89 | 529 | goto out_put; |
09e10322 | 530 | } |
3d3c6b89 MS |
531 | if (!this) |
532 | continue; | |
3e01cee3 MS |
533 | if (ovl_is_whiteout(this)) { |
534 | dput(this); | |
535 | break; | |
536 | } | |
3d3c6b89 | 537 | /* |
8ee6059c | 538 | * If this is a non-directory then stop here. |
3d3c6b89 | 539 | */ |
8ee6059c MS |
540 | if (!d_is_dir(this)) { |
541 | if (isdir) { | |
542 | dput(this); | |
543 | break; | |
544 | } | |
545 | stop = true; | |
546 | } else { | |
a425c037 | 547 | /* |
8ee6059c MS |
548 | * Only makes sense to check opaque dir if this is not |
549 | * the lowermost layer. | |
a425c037 | 550 | */ |
8ee6059c MS |
551 | if (i < poe->numlower - 1 && ovl_is_opaquedir(this)) |
552 | stop = true; | |
3d3c6b89 | 553 | } |
a425c037 | 554 | |
3d3c6b89 MS |
555 | stack[ctr].dentry = this; |
556 | stack[ctr].mnt = lowerpath.mnt; | |
557 | ctr++; | |
e9be9d5e MS |
558 | } |
559 | ||
3d3c6b89 MS |
560 | oe = ovl_alloc_entry(ctr); |
561 | err = -ENOMEM; | |
562 | if (!oe) | |
563 | goto out_put; | |
564 | ||
565 | if (upperdentry || ctr) { | |
e9be9d5e | 566 | struct dentry *realdentry; |
39b681f8 | 567 | struct inode *realinode; |
e9be9d5e | 568 | |
3d3c6b89 | 569 | realdentry = upperdentry ? upperdentry : stack[0].dentry; |
39b681f8 | 570 | realinode = d_inode(realdentry); |
3d3c6b89 | 571 | |
e9be9d5e | 572 | err = -ENOMEM; |
51f7e52d MS |
573 | if (upperdentry && !d_is_dir(upperdentry)) { |
574 | inode = ovl_get_inode(dentry->d_sb, realinode); | |
575 | } else { | |
ca4c8a3a MS |
576 | inode = ovl_new_inode(dentry->d_sb, realinode->i_mode, |
577 | realinode->i_rdev); | |
51f7e52d MS |
578 | if (inode) |
579 | ovl_inode_init(inode, realinode, !!upperdentry); | |
580 | } | |
e9be9d5e | 581 | if (!inode) |
3d3c6b89 | 582 | goto out_free_oe; |
e9be9d5e MS |
583 | ovl_copyattr(realdentry->d_inode, inode); |
584 | } | |
585 | ||
2b6bc7f4 | 586 | revert_creds(old_cred); |
3d3c6b89 | 587 | oe->opaque = upperopaque; |
e9be9d5e | 588 | oe->__upperdentry = upperdentry; |
3d3c6b89 MS |
589 | memcpy(oe->lowerstack, stack, sizeof(struct path) * ctr); |
590 | kfree(stack); | |
e9be9d5e MS |
591 | dentry->d_fsdata = oe; |
592 | d_add(dentry, inode); | |
593 | ||
594 | return NULL; | |
595 | ||
3d3c6b89 | 596 | out_free_oe: |
e9be9d5e | 597 | kfree(oe); |
3d3c6b89 MS |
598 | out_put: |
599 | for (i = 0; i < ctr; i++) | |
600 | dput(stack[i].dentry); | |
601 | kfree(stack); | |
602 | out_put_upper: | |
603 | dput(upperdentry); | |
e9be9d5e | 604 | out: |
2b6bc7f4 | 605 | revert_creds(old_cred); |
e9be9d5e MS |
606 | return ERR_PTR(err); |
607 | } | |
608 | ||
2aff4534 MS |
609 | bool ovl_lower_positive(struct dentry *dentry) |
610 | { | |
611 | struct ovl_entry *oe = dentry->d_fsdata; | |
612 | struct ovl_entry *poe = dentry->d_parent->d_fsdata; | |
613 | const struct qstr *name = &dentry->d_name; | |
614 | unsigned int i; | |
615 | bool positive = false; | |
616 | bool done = false; | |
617 | ||
618 | /* | |
619 | * If dentry is negative, then lower is positive iff this is a | |
620 | * whiteout. | |
621 | */ | |
622 | if (!dentry->d_inode) | |
623 | return oe->opaque; | |
624 | ||
625 | /* Negative upper -> positive lower */ | |
626 | if (!oe->__upperdentry) | |
627 | return true; | |
628 | ||
629 | /* Positive upper -> have to look up lower to see whether it exists */ | |
630 | for (i = 0; !done && !positive && i < poe->numlower; i++) { | |
631 | struct dentry *this; | |
632 | struct dentry *lowerdir = poe->lowerstack[i].dentry; | |
633 | ||
634 | this = lookup_one_len_unlocked(name->name, lowerdir, | |
635 | name->len); | |
636 | if (IS_ERR(this)) { | |
637 | switch (PTR_ERR(this)) { | |
638 | case -ENOENT: | |
639 | case -ENAMETOOLONG: | |
640 | break; | |
641 | ||
642 | default: | |
643 | /* | |
644 | * Assume something is there, we just couldn't | |
645 | * access it. | |
646 | */ | |
647 | positive = true; | |
648 | break; | |
649 | } | |
650 | } else { | |
651 | if (this->d_inode) { | |
652 | positive = !ovl_is_whiteout(this); | |
653 | done = true; | |
654 | } | |
655 | dput(this); | |
656 | } | |
657 | } | |
658 | ||
659 | return positive; | |
660 | } | |
661 | ||
e9be9d5e MS |
662 | struct file *ovl_path_open(struct path *path, int flags) |
663 | { | |
d719e8f2 | 664 | return dentry_open(path, flags | O_NOATIME, current_cred()); |
e9be9d5e MS |
665 | } |
666 | ||
667 | static void ovl_put_super(struct super_block *sb) | |
668 | { | |
669 | struct ovl_fs *ufs = sb->s_fs_info; | |
dd662667 | 670 | unsigned i; |
e9be9d5e MS |
671 | |
672 | dput(ufs->workdir); | |
673 | mntput(ufs->upper_mnt); | |
dd662667 MS |
674 | for (i = 0; i < ufs->numlower; i++) |
675 | mntput(ufs->lower_mnt[i]); | |
5ffdbe8b | 676 | kfree(ufs->lower_mnt); |
e9be9d5e | 677 | |
f45827e8 EZ |
678 | kfree(ufs->config.lowerdir); |
679 | kfree(ufs->config.upperdir); | |
680 | kfree(ufs->config.workdir); | |
3fe6e52f | 681 | put_cred(ufs->creator_cred); |
e9be9d5e MS |
682 | kfree(ufs); |
683 | } | |
684 | ||
cc259639 AW |
685 | /** |
686 | * ovl_statfs | |
687 | * @sb: The overlayfs super block | |
688 | * @buf: The struct kstatfs to fill in with stats | |
689 | * | |
690 | * Get the filesystem statistics. As writes always target the upper layer | |
4ebc5818 | 691 | * filesystem pass the statfs to the upper filesystem (if it exists) |
cc259639 AW |
692 | */ |
693 | static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf) | |
694 | { | |
695 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; | |
696 | struct dentry *root_dentry = dentry->d_sb->s_root; | |
697 | struct path path; | |
698 | int err; | |
699 | ||
4ebc5818 | 700 | ovl_path_real(root_dentry, &path); |
cc259639 AW |
701 | |
702 | err = vfs_statfs(&path, buf); | |
703 | if (!err) { | |
704 | buf->f_namelen = max(buf->f_namelen, ofs->lower_namelen); | |
705 | buf->f_type = OVERLAYFS_SUPER_MAGIC; | |
706 | } | |
707 | ||
708 | return err; | |
709 | } | |
710 | ||
f45827e8 EZ |
711 | /** |
712 | * ovl_show_options | |
713 | * | |
714 | * Prints the mount options for a given superblock. | |
715 | * Returns zero; does not fail. | |
716 | */ | |
717 | static int ovl_show_options(struct seq_file *m, struct dentry *dentry) | |
718 | { | |
719 | struct super_block *sb = dentry->d_sb; | |
720 | struct ovl_fs *ufs = sb->s_fs_info; | |
721 | ||
a068acf2 | 722 | seq_show_option(m, "lowerdir", ufs->config.lowerdir); |
53a08cb9 | 723 | if (ufs->config.upperdir) { |
a068acf2 KC |
724 | seq_show_option(m, "upperdir", ufs->config.upperdir); |
725 | seq_show_option(m, "workdir", ufs->config.workdir); | |
53a08cb9 | 726 | } |
8d3095f4 MS |
727 | if (ufs->config.default_permissions) |
728 | seq_puts(m, ",default_permissions"); | |
f45827e8 EZ |
729 | return 0; |
730 | } | |
731 | ||
3cdf6fe9 SL |
732 | static int ovl_remount(struct super_block *sb, int *flags, char *data) |
733 | { | |
734 | struct ovl_fs *ufs = sb->s_fs_info; | |
735 | ||
cc6f67bc | 736 | if (!(*flags & MS_RDONLY) && (!ufs->upper_mnt || !ufs->workdir)) |
3cdf6fe9 SL |
737 | return -EROFS; |
738 | ||
739 | return 0; | |
740 | } | |
741 | ||
e9be9d5e MS |
742 | static const struct super_operations ovl_super_operations = { |
743 | .put_super = ovl_put_super, | |
cc259639 | 744 | .statfs = ovl_statfs, |
f45827e8 | 745 | .show_options = ovl_show_options, |
3cdf6fe9 | 746 | .remount_fs = ovl_remount, |
eead4f2d | 747 | .drop_inode = generic_delete_inode, |
e9be9d5e MS |
748 | }; |
749 | ||
750 | enum { | |
751 | OPT_LOWERDIR, | |
752 | OPT_UPPERDIR, | |
753 | OPT_WORKDIR, | |
8d3095f4 | 754 | OPT_DEFAULT_PERMISSIONS, |
e9be9d5e MS |
755 | OPT_ERR, |
756 | }; | |
757 | ||
758 | static const match_table_t ovl_tokens = { | |
759 | {OPT_LOWERDIR, "lowerdir=%s"}, | |
760 | {OPT_UPPERDIR, "upperdir=%s"}, | |
761 | {OPT_WORKDIR, "workdir=%s"}, | |
8d3095f4 | 762 | {OPT_DEFAULT_PERMISSIONS, "default_permissions"}, |
e9be9d5e MS |
763 | {OPT_ERR, NULL} |
764 | }; | |
765 | ||
91c77947 MS |
766 | static char *ovl_next_opt(char **s) |
767 | { | |
768 | char *sbegin = *s; | |
769 | char *p; | |
770 | ||
771 | if (sbegin == NULL) | |
772 | return NULL; | |
773 | ||
774 | for (p = sbegin; *p; p++) { | |
775 | if (*p == '\\') { | |
776 | p++; | |
777 | if (!*p) | |
778 | break; | |
779 | } else if (*p == ',') { | |
780 | *p = '\0'; | |
781 | *s = p + 1; | |
782 | return sbegin; | |
783 | } | |
784 | } | |
785 | *s = NULL; | |
786 | return sbegin; | |
787 | } | |
788 | ||
e9be9d5e MS |
789 | static int ovl_parse_opt(char *opt, struct ovl_config *config) |
790 | { | |
791 | char *p; | |
792 | ||
91c77947 | 793 | while ((p = ovl_next_opt(&opt)) != NULL) { |
e9be9d5e MS |
794 | int token; |
795 | substring_t args[MAX_OPT_ARGS]; | |
796 | ||
797 | if (!*p) | |
798 | continue; | |
799 | ||
800 | token = match_token(p, ovl_tokens, args); | |
801 | switch (token) { | |
802 | case OPT_UPPERDIR: | |
803 | kfree(config->upperdir); | |
804 | config->upperdir = match_strdup(&args[0]); | |
805 | if (!config->upperdir) | |
806 | return -ENOMEM; | |
807 | break; | |
808 | ||
809 | case OPT_LOWERDIR: | |
810 | kfree(config->lowerdir); | |
811 | config->lowerdir = match_strdup(&args[0]); | |
812 | if (!config->lowerdir) | |
813 | return -ENOMEM; | |
814 | break; | |
815 | ||
816 | case OPT_WORKDIR: | |
817 | kfree(config->workdir); | |
818 | config->workdir = match_strdup(&args[0]); | |
819 | if (!config->workdir) | |
820 | return -ENOMEM; | |
821 | break; | |
822 | ||
8d3095f4 MS |
823 | case OPT_DEFAULT_PERMISSIONS: |
824 | config->default_permissions = true; | |
825 | break; | |
826 | ||
e9be9d5e | 827 | default: |
bead55ef | 828 | pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p); |
e9be9d5e MS |
829 | return -EINVAL; |
830 | } | |
831 | } | |
71cbad7e | 832 | |
833 | /* Workdir is useless in non-upper mount */ | |
834 | if (!config->upperdir && config->workdir) { | |
835 | pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n", | |
836 | config->workdir); | |
837 | kfree(config->workdir); | |
838 | config->workdir = NULL; | |
839 | } | |
840 | ||
e9be9d5e MS |
841 | return 0; |
842 | } | |
843 | ||
844 | #define OVL_WORKDIR_NAME "work" | |
845 | ||
846 | static struct dentry *ovl_workdir_create(struct vfsmount *mnt, | |
847 | struct dentry *dentry) | |
848 | { | |
849 | struct inode *dir = dentry->d_inode; | |
850 | struct dentry *work; | |
851 | int err; | |
852 | bool retried = false; | |
853 | ||
854 | err = mnt_want_write(mnt); | |
855 | if (err) | |
856 | return ERR_PTR(err); | |
857 | ||
5955102c | 858 | inode_lock_nested(dir, I_MUTEX_PARENT); |
e9be9d5e MS |
859 | retry: |
860 | work = lookup_one_len(OVL_WORKDIR_NAME, dentry, | |
861 | strlen(OVL_WORKDIR_NAME)); | |
862 | ||
863 | if (!IS_ERR(work)) { | |
864 | struct kstat stat = { | |
865 | .mode = S_IFDIR | 0, | |
866 | }; | |
c11b9fdd MS |
867 | struct iattr attr = { |
868 | .ia_valid = ATTR_MODE, | |
869 | .ia_mode = stat.mode, | |
870 | }; | |
e9be9d5e MS |
871 | |
872 | if (work->d_inode) { | |
873 | err = -EEXIST; | |
874 | if (retried) | |
875 | goto out_dput; | |
876 | ||
877 | retried = true; | |
eea2fb48 | 878 | ovl_workdir_cleanup(dir, mnt, work, 0); |
e9be9d5e MS |
879 | dput(work); |
880 | goto retry; | |
881 | } | |
882 | ||
883 | err = ovl_create_real(dir, work, &stat, NULL, NULL, true); | |
884 | if (err) | |
885 | goto out_dput; | |
c11b9fdd | 886 | |
cb348edb MS |
887 | /* |
888 | * Try to remove POSIX ACL xattrs from workdir. We are good if: | |
889 | * | |
890 | * a) success (there was a POSIX ACL xattr and was removed) | |
891 | * b) -ENODATA (there was no POSIX ACL xattr) | |
892 | * c) -EOPNOTSUPP (POSIX ACL xattrs are not supported) | |
893 | * | |
894 | * There are various other error values that could effectively | |
895 | * mean that the xattr doesn't exist (e.g. -ERANGE is returned | |
896 | * if the xattr name is too long), but the set of filesystems | |
897 | * allowed as upper are limited to "normal" ones, where checking | |
898 | * for the above two errors is sufficient. | |
899 | */ | |
c11b9fdd | 900 | err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_DEFAULT); |
e1ff3dd1 | 901 | if (err && err != -ENODATA && err != -EOPNOTSUPP) |
c11b9fdd MS |
902 | goto out_dput; |
903 | ||
904 | err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_ACCESS); | |
e1ff3dd1 | 905 | if (err && err != -ENODATA && err != -EOPNOTSUPP) |
c11b9fdd MS |
906 | goto out_dput; |
907 | ||
908 | /* Clear any inherited mode bits */ | |
909 | inode_lock(work->d_inode); | |
910 | err = notify_change(work, &attr, NULL); | |
911 | inode_unlock(work->d_inode); | |
912 | if (err) | |
913 | goto out_dput; | |
e9be9d5e MS |
914 | } |
915 | out_unlock: | |
5955102c | 916 | inode_unlock(dir); |
e9be9d5e MS |
917 | mnt_drop_write(mnt); |
918 | ||
919 | return work; | |
920 | ||
921 | out_dput: | |
922 | dput(work); | |
923 | work = ERR_PTR(err); | |
924 | goto out_unlock; | |
925 | } | |
926 | ||
91c77947 MS |
927 | static void ovl_unescape(char *s) |
928 | { | |
929 | char *d = s; | |
930 | ||
931 | for (;; s++, d++) { | |
932 | if (*s == '\\') | |
933 | s++; | |
934 | *d = *s; | |
935 | if (!*s) | |
936 | break; | |
937 | } | |
938 | } | |
939 | ||
ab508822 MS |
940 | static int ovl_mount_dir_noesc(const char *name, struct path *path) |
941 | { | |
a78d9f0d | 942 | int err = -EINVAL; |
ab508822 | 943 | |
a78d9f0d MS |
944 | if (!*name) { |
945 | pr_err("overlayfs: empty lowerdir\n"); | |
946 | goto out; | |
947 | } | |
ab508822 MS |
948 | err = kern_path(name, LOOKUP_FOLLOW, path); |
949 | if (err) { | |
950 | pr_err("overlayfs: failed to resolve '%s': %i\n", name, err); | |
951 | goto out; | |
952 | } | |
953 | err = -EINVAL; | |
7c03b5d4 | 954 | if (ovl_dentry_weird(path->dentry)) { |
ab508822 MS |
955 | pr_err("overlayfs: filesystem on '%s' not supported\n", name); |
956 | goto out_put; | |
957 | } | |
958 | if (!S_ISDIR(path->dentry->d_inode->i_mode)) { | |
959 | pr_err("overlayfs: '%s' not a directory\n", name); | |
960 | goto out_put; | |
961 | } | |
962 | return 0; | |
963 | ||
964 | out_put: | |
965 | path_put(path); | |
966 | out: | |
967 | return err; | |
968 | } | |
969 | ||
970 | static int ovl_mount_dir(const char *name, struct path *path) | |
971 | { | |
972 | int err = -ENOMEM; | |
973 | char *tmp = kstrdup(name, GFP_KERNEL); | |
974 | ||
975 | if (tmp) { | |
976 | ovl_unescape(tmp); | |
977 | err = ovl_mount_dir_noesc(tmp, path); | |
7c03b5d4 MS |
978 | |
979 | if (!err) | |
980 | if (ovl_dentry_remote(path->dentry)) { | |
981 | pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n", | |
982 | tmp); | |
983 | path_put(path); | |
984 | err = -EINVAL; | |
985 | } | |
ab508822 MS |
986 | kfree(tmp); |
987 | } | |
988 | return err; | |
989 | } | |
990 | ||
991 | static int ovl_lower_dir(const char *name, struct path *path, long *namelen, | |
7c03b5d4 | 992 | int *stack_depth, bool *remote) |
ab508822 MS |
993 | { |
994 | int err; | |
995 | struct kstatfs statfs; | |
996 | ||
a78d9f0d | 997 | err = ovl_mount_dir_noesc(name, path); |
ab508822 MS |
998 | if (err) |
999 | goto out; | |
1000 | ||
1001 | err = vfs_statfs(path, &statfs); | |
1002 | if (err) { | |
1003 | pr_err("overlayfs: statfs failed on '%s'\n", name); | |
1004 | goto out_put; | |
1005 | } | |
1006 | *namelen = max(*namelen, statfs.f_namelen); | |
1007 | *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth); | |
1008 | ||
7c03b5d4 MS |
1009 | if (ovl_dentry_remote(path->dentry)) |
1010 | *remote = true; | |
1011 | ||
ab508822 MS |
1012 | return 0; |
1013 | ||
1014 | out_put: | |
1015 | path_put(path); | |
1016 | out: | |
1017 | return err; | |
1018 | } | |
1019 | ||
e9be9d5e MS |
1020 | /* Workdir should not be subdir of upperdir and vice versa */ |
1021 | static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir) | |
1022 | { | |
1023 | bool ok = false; | |
1024 | ||
1025 | if (workdir != upperdir) { | |
1026 | ok = (lock_rename(workdir, upperdir) == NULL); | |
1027 | unlock_rename(workdir, upperdir); | |
1028 | } | |
1029 | return ok; | |
1030 | } | |
1031 | ||
a78d9f0d MS |
1032 | static unsigned int ovl_split_lowerdirs(char *str) |
1033 | { | |
1034 | unsigned int ctr = 1; | |
1035 | char *s, *d; | |
1036 | ||
1037 | for (s = d = str;; s++, d++) { | |
1038 | if (*s == '\\') { | |
1039 | s++; | |
1040 | } else if (*s == ':') { | |
1041 | *d = '\0'; | |
1042 | ctr++; | |
1043 | continue; | |
1044 | } | |
1045 | *d = *s; | |
1046 | if (!*s) | |
1047 | break; | |
1048 | } | |
1049 | return ctr; | |
1050 | } | |
1051 | ||
0eb45fc3 AG |
1052 | static int __maybe_unused |
1053 | ovl_posix_acl_xattr_get(const struct xattr_handler *handler, | |
1054 | struct dentry *dentry, struct inode *inode, | |
1055 | const char *name, void *buffer, size_t size) | |
1056 | { | |
1057 | return ovl_xattr_get(dentry, handler->name, buffer, size); | |
1058 | } | |
1059 | ||
0c97be22 AG |
1060 | static int __maybe_unused |
1061 | ovl_posix_acl_xattr_set(const struct xattr_handler *handler, | |
1062 | struct dentry *dentry, struct inode *inode, | |
1063 | const char *name, const void *value, | |
1064 | size_t size, int flags) | |
d837a49b MS |
1065 | { |
1066 | struct dentry *workdir = ovl_workdir(dentry); | |
1067 | struct inode *realinode = ovl_inode_real(inode, NULL); | |
1068 | struct posix_acl *acl = NULL; | |
1069 | int err; | |
1070 | ||
1071 | /* Check that everything is OK before copy-up */ | |
1072 | if (value) { | |
1073 | acl = posix_acl_from_xattr(&init_user_ns, value, size); | |
1074 | if (IS_ERR(acl)) | |
1075 | return PTR_ERR(acl); | |
1076 | } | |
1077 | err = -EOPNOTSUPP; | |
1078 | if (!IS_POSIXACL(d_inode(workdir))) | |
1079 | goto out_acl_release; | |
1080 | if (!realinode->i_op->set_acl) | |
1081 | goto out_acl_release; | |
1082 | if (handler->flags == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) { | |
1083 | err = acl ? -EACCES : 0; | |
1084 | goto out_acl_release; | |
1085 | } | |
1086 | err = -EPERM; | |
1087 | if (!inode_owner_or_capable(inode)) | |
1088 | goto out_acl_release; | |
1089 | ||
1090 | posix_acl_release(acl); | |
1091 | ||
fd3220d3 MS |
1092 | /* |
1093 | * Check if sgid bit needs to be cleared (actual setacl operation will | |
1094 | * be done with mounter's capabilities and so that won't do it for us). | |
1095 | */ | |
1096 | if (unlikely(inode->i_mode & S_ISGID) && | |
1097 | handler->flags == ACL_TYPE_ACCESS && | |
1098 | !in_group_p(inode->i_gid) && | |
1099 | !capable_wrt_inode_uidgid(inode, CAP_FSETID)) { | |
1100 | struct iattr iattr = { .ia_valid = ATTR_KILL_SGID }; | |
1101 | ||
1102 | err = ovl_setattr(dentry, &iattr); | |
1103 | if (err) | |
1104 | return err; | |
1105 | } | |
1106 | ||
ce31513a MS |
1107 | err = ovl_xattr_set(dentry, handler->name, value, size, flags); |
1108 | if (!err) | |
1109 | ovl_copyattr(ovl_inode_real(inode, NULL), inode); | |
1110 | ||
1111 | return err; | |
d837a49b MS |
1112 | |
1113 | out_acl_release: | |
1114 | posix_acl_release(acl); | |
1115 | return err; | |
1116 | } | |
1117 | ||
0eb45fc3 AG |
1118 | static int ovl_own_xattr_get(const struct xattr_handler *handler, |
1119 | struct dentry *dentry, struct inode *inode, | |
1120 | const char *name, void *buffer, size_t size) | |
1121 | { | |
1122 | return -EPERM; | |
1123 | } | |
1124 | ||
d837a49b MS |
1125 | static int ovl_own_xattr_set(const struct xattr_handler *handler, |
1126 | struct dentry *dentry, struct inode *inode, | |
1127 | const char *name, const void *value, | |
1128 | size_t size, int flags) | |
1129 | { | |
1130 | return -EPERM; | |
1131 | } | |
1132 | ||
0eb45fc3 AG |
1133 | static int ovl_other_xattr_get(const struct xattr_handler *handler, |
1134 | struct dentry *dentry, struct inode *inode, | |
1135 | const char *name, void *buffer, size_t size) | |
1136 | { | |
1137 | return ovl_xattr_get(dentry, name, buffer, size); | |
1138 | } | |
1139 | ||
0e585ccc AG |
1140 | static int ovl_other_xattr_set(const struct xattr_handler *handler, |
1141 | struct dentry *dentry, struct inode *inode, | |
1142 | const char *name, const void *value, | |
1143 | size_t size, int flags) | |
1144 | { | |
1145 | return ovl_xattr_set(dentry, name, value, size, flags); | |
1146 | } | |
1147 | ||
0c97be22 AG |
1148 | static const struct xattr_handler __maybe_unused |
1149 | ovl_posix_acl_access_xattr_handler = { | |
d837a49b MS |
1150 | .name = XATTR_NAME_POSIX_ACL_ACCESS, |
1151 | .flags = ACL_TYPE_ACCESS, | |
0eb45fc3 | 1152 | .get = ovl_posix_acl_xattr_get, |
d837a49b MS |
1153 | .set = ovl_posix_acl_xattr_set, |
1154 | }; | |
1155 | ||
0c97be22 AG |
1156 | static const struct xattr_handler __maybe_unused |
1157 | ovl_posix_acl_default_xattr_handler = { | |
d837a49b MS |
1158 | .name = XATTR_NAME_POSIX_ACL_DEFAULT, |
1159 | .flags = ACL_TYPE_DEFAULT, | |
0eb45fc3 | 1160 | .get = ovl_posix_acl_xattr_get, |
d837a49b MS |
1161 | .set = ovl_posix_acl_xattr_set, |
1162 | }; | |
1163 | ||
1164 | static const struct xattr_handler ovl_own_xattr_handler = { | |
1165 | .prefix = OVL_XATTR_PREFIX, | |
0eb45fc3 | 1166 | .get = ovl_own_xattr_get, |
d837a49b MS |
1167 | .set = ovl_own_xattr_set, |
1168 | }; | |
1169 | ||
1170 | static const struct xattr_handler ovl_other_xattr_handler = { | |
1171 | .prefix = "", /* catch all */ | |
0eb45fc3 | 1172 | .get = ovl_other_xattr_get, |
d837a49b MS |
1173 | .set = ovl_other_xattr_set, |
1174 | }; | |
1175 | ||
1176 | static const struct xattr_handler *ovl_xattr_handlers[] = { | |
0c97be22 | 1177 | #ifdef CONFIG_FS_POSIX_ACL |
d837a49b MS |
1178 | &ovl_posix_acl_access_xattr_handler, |
1179 | &ovl_posix_acl_default_xattr_handler, | |
0c97be22 | 1180 | #endif |
d837a49b MS |
1181 | &ovl_own_xattr_handler, |
1182 | &ovl_other_xattr_handler, | |
1183 | NULL | |
1184 | }; | |
1185 | ||
e9be9d5e MS |
1186 | static int ovl_fill_super(struct super_block *sb, void *data, int silent) |
1187 | { | |
53a08cb9 MS |
1188 | struct path upperpath = { NULL, NULL }; |
1189 | struct path workpath = { NULL, NULL }; | |
e9be9d5e | 1190 | struct dentry *root_dentry; |
39b681f8 | 1191 | struct inode *realinode; |
e9be9d5e MS |
1192 | struct ovl_entry *oe; |
1193 | struct ovl_fs *ufs; | |
a78d9f0d MS |
1194 | struct path *stack = NULL; |
1195 | char *lowertmp; | |
1196 | char *lower; | |
1197 | unsigned int numlower; | |
1198 | unsigned int stacklen = 0; | |
dd662667 | 1199 | unsigned int i; |
7c03b5d4 | 1200 | bool remote = false; |
e9be9d5e MS |
1201 | int err; |
1202 | ||
f45827e8 EZ |
1203 | err = -ENOMEM; |
1204 | ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL); | |
1205 | if (!ufs) | |
e9be9d5e MS |
1206 | goto out; |
1207 | ||
f45827e8 EZ |
1208 | err = ovl_parse_opt((char *) data, &ufs->config); |
1209 | if (err) | |
1210 | goto out_free_config; | |
1211 | ||
e9be9d5e | 1212 | err = -EINVAL; |
53a08cb9 | 1213 | if (!ufs->config.lowerdir) { |
07f2af7b KK |
1214 | if (!silent) |
1215 | pr_err("overlayfs: missing 'lowerdir'\n"); | |
e9be9d5e MS |
1216 | goto out_free_config; |
1217 | } | |
1218 | ||
53a08cb9 | 1219 | sb->s_stack_depth = 0; |
cf9a6784 | 1220 | sb->s_maxbytes = MAX_LFS_FILESIZE; |
53a08cb9 | 1221 | if (ufs->config.upperdir) { |
53a08cb9 MS |
1222 | if (!ufs->config.workdir) { |
1223 | pr_err("overlayfs: missing 'workdir'\n"); | |
1224 | goto out_free_config; | |
1225 | } | |
e9be9d5e | 1226 | |
53a08cb9 MS |
1227 | err = ovl_mount_dir(ufs->config.upperdir, &upperpath); |
1228 | if (err) | |
1229 | goto out_free_config; | |
e9be9d5e | 1230 | |
71cbad7e | 1231 | /* Upper fs should not be r/o */ |
1232 | if (upperpath.mnt->mnt_sb->s_flags & MS_RDONLY) { | |
1233 | pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n"); | |
1234 | err = -EINVAL; | |
1235 | goto out_put_upperpath; | |
1236 | } | |
1237 | ||
53a08cb9 MS |
1238 | err = ovl_mount_dir(ufs->config.workdir, &workpath); |
1239 | if (err) | |
1240 | goto out_put_upperpath; | |
1241 | ||
2f83fd8c | 1242 | err = -EINVAL; |
53a08cb9 MS |
1243 | if (upperpath.mnt != workpath.mnt) { |
1244 | pr_err("overlayfs: workdir and upperdir must reside under the same mount\n"); | |
1245 | goto out_put_workpath; | |
1246 | } | |
1247 | if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) { | |
1248 | pr_err("overlayfs: workdir and upperdir must be separate subtrees\n"); | |
1249 | goto out_put_workpath; | |
1250 | } | |
1251 | sb->s_stack_depth = upperpath.mnt->mnt_sb->s_stack_depth; | |
cc259639 | 1252 | } |
a78d9f0d MS |
1253 | err = -ENOMEM; |
1254 | lowertmp = kstrdup(ufs->config.lowerdir, GFP_KERNEL); | |
1255 | if (!lowertmp) | |
ab508822 | 1256 | goto out_put_workpath; |
69c433ed | 1257 | |
a78d9f0d MS |
1258 | err = -EINVAL; |
1259 | stacklen = ovl_split_lowerdirs(lowertmp); | |
6be4506e | 1260 | if (stacklen > OVL_MAX_STACK) { |
fd36570a | 1261 | pr_err("overlayfs: too many lower directories, limit is %d\n", |
6be4506e | 1262 | OVL_MAX_STACK); |
a78d9f0d | 1263 | goto out_free_lowertmp; |
6be4506e | 1264 | } else if (!ufs->config.upperdir && stacklen == 1) { |
1265 | pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n"); | |
1266 | goto out_free_lowertmp; | |
1267 | } | |
a78d9f0d MS |
1268 | |
1269 | stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL); | |
1270 | if (!stack) | |
1271 | goto out_free_lowertmp; | |
1272 | ||
1273 | lower = lowertmp; | |
1274 | for (numlower = 0; numlower < stacklen; numlower++) { | |
1275 | err = ovl_lower_dir(lower, &stack[numlower], | |
7c03b5d4 MS |
1276 | &ufs->lower_namelen, &sb->s_stack_depth, |
1277 | &remote); | |
a78d9f0d MS |
1278 | if (err) |
1279 | goto out_put_lowerpath; | |
1280 | ||
1281 | lower = strchr(lower, '\0') + 1; | |
1282 | } | |
1283 | ||
69c433ed | 1284 | err = -EINVAL; |
ab508822 | 1285 | sb->s_stack_depth++; |
69c433ed MS |
1286 | if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) { |
1287 | pr_err("overlayfs: maximum fs stacking depth exceeded\n"); | |
3b7a9a24 | 1288 | goto out_put_lowerpath; |
69c433ed MS |
1289 | } |
1290 | ||
53a08cb9 MS |
1291 | if (ufs->config.upperdir) { |
1292 | ufs->upper_mnt = clone_private_mount(&upperpath); | |
1293 | err = PTR_ERR(ufs->upper_mnt); | |
1294 | if (IS_ERR(ufs->upper_mnt)) { | |
1295 | pr_err("overlayfs: failed to clone upperpath\n"); | |
1296 | goto out_put_lowerpath; | |
1297 | } | |
d719e8f2 MS |
1298 | /* Don't inherit atime flags */ |
1299 | ufs->upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME); | |
1300 | ||
1301 | sb->s_time_gran = ufs->upper_mnt->mnt_sb->s_time_gran; | |
3b7a9a24 | 1302 | |
53a08cb9 MS |
1303 | ufs->workdir = ovl_workdir_create(ufs->upper_mnt, workpath.dentry); |
1304 | err = PTR_ERR(ufs->workdir); | |
1305 | if (IS_ERR(ufs->workdir)) { | |
cc6f67bc MS |
1306 | pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n", |
1307 | ufs->config.workdir, OVL_WORKDIR_NAME, -err); | |
1308 | sb->s_flags |= MS_RDONLY; | |
1309 | ufs->workdir = NULL; | |
53a08cb9 | 1310 | } |
45aebeaf VG |
1311 | |
1312 | /* | |
1313 | * Upper should support d_type, else whiteouts are visible. | |
1314 | * Given workdir and upper are on same fs, we can do | |
21765194 VG |
1315 | * iterate_dir() on workdir. This check requires successful |
1316 | * creation of workdir in previous step. | |
45aebeaf | 1317 | */ |
21765194 VG |
1318 | if (ufs->workdir) { |
1319 | err = ovl_check_d_type_supported(&workpath); | |
1320 | if (err < 0) | |
1321 | goto out_put_workdir; | |
45aebeaf | 1322 | |
e7c0b599 VG |
1323 | /* |
1324 | * We allowed this configuration and don't want to | |
1325 | * break users over kernel upgrade. So warn instead | |
1326 | * of erroring out. | |
1327 | */ | |
1328 | if (!err) | |
1329 | pr_warn("overlayfs: upper fs needs to support d_type.\n"); | |
45aebeaf | 1330 | } |
e9be9d5e MS |
1331 | } |
1332 | ||
2f83fd8c | 1333 | err = -ENOMEM; |
a78d9f0d | 1334 | ufs->lower_mnt = kcalloc(numlower, sizeof(struct vfsmount *), GFP_KERNEL); |
dd662667 | 1335 | if (ufs->lower_mnt == NULL) |
3b7a9a24 | 1336 | goto out_put_workdir; |
a78d9f0d MS |
1337 | for (i = 0; i < numlower; i++) { |
1338 | struct vfsmount *mnt = clone_private_mount(&stack[i]); | |
dd662667 | 1339 | |
2f83fd8c | 1340 | err = PTR_ERR(mnt); |
a78d9f0d MS |
1341 | if (IS_ERR(mnt)) { |
1342 | pr_err("overlayfs: failed to clone lowerpath\n"); | |
1343 | goto out_put_lower_mnt; | |
1344 | } | |
1345 | /* | |
1346 | * Make lower_mnt R/O. That way fchmod/fchown on lower file | |
1347 | * will fail instead of modifying lower fs. | |
1348 | */ | |
d719e8f2 | 1349 | mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME; |
dd662667 | 1350 | |
a78d9f0d MS |
1351 | ufs->lower_mnt[ufs->numlower] = mnt; |
1352 | ufs->numlower++; | |
1353 | } | |
e9be9d5e | 1354 | |
71cbad7e | 1355 | /* If the upper fs is nonexistent, we mark overlayfs r/o too */ |
1356 | if (!ufs->upper_mnt) | |
e9be9d5e MS |
1357 | sb->s_flags |= MS_RDONLY; |
1358 | ||
7c03b5d4 MS |
1359 | if (remote) |
1360 | sb->s_d_op = &ovl_reval_dentry_operations; | |
1361 | else | |
1362 | sb->s_d_op = &ovl_dentry_operations; | |
e9be9d5e | 1363 | |
3fe6e52f AM |
1364 | ufs->creator_cred = prepare_creds(); |
1365 | if (!ufs->creator_cred) | |
1366 | goto out_put_lower_mnt; | |
1367 | ||
e9be9d5e | 1368 | err = -ENOMEM; |
a78d9f0d | 1369 | oe = ovl_alloc_entry(numlower); |
3b7a9a24 | 1370 | if (!oe) |
3fe6e52f | 1371 | goto out_put_cred; |
e9be9d5e | 1372 | |
655042cc VG |
1373 | sb->s_magic = OVERLAYFS_SUPER_MAGIC; |
1374 | sb->s_op = &ovl_super_operations; | |
1375 | sb->s_xattr = ovl_xattr_handlers; | |
1376 | sb->s_fs_info = ufs; | |
1377 | sb->s_flags |= MS_POSIXACL | MS_NOREMOTELOCK; | |
1378 | ||
ca4c8a3a | 1379 | root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, 0)); |
e9be9d5e | 1380 | if (!root_dentry) |
3b7a9a24 | 1381 | goto out_free_oe; |
e9be9d5e MS |
1382 | |
1383 | mntput(upperpath.mnt); | |
a78d9f0d MS |
1384 | for (i = 0; i < numlower; i++) |
1385 | mntput(stack[i].mnt); | |
e9be9d5e | 1386 | path_put(&workpath); |
a78d9f0d | 1387 | kfree(lowertmp); |
e9be9d5e MS |
1388 | |
1389 | oe->__upperdentry = upperpath.dentry; | |
a78d9f0d MS |
1390 | for (i = 0; i < numlower; i++) { |
1391 | oe->lowerstack[i].dentry = stack[i].dentry; | |
1392 | oe->lowerstack[i].mnt = ufs->lower_mnt[i]; | |
1393 | } | |
0f95502a | 1394 | kfree(stack); |
e9be9d5e MS |
1395 | |
1396 | root_dentry->d_fsdata = oe; | |
1397 | ||
39b681f8 MS |
1398 | realinode = d_inode(ovl_dentry_real(root_dentry)); |
1399 | ovl_inode_init(d_inode(root_dentry), realinode, !!upperpath.dentry); | |
1400 | ovl_copyattr(realinode, d_inode(root_dentry)); | |
ed06e069 | 1401 | |
e9be9d5e | 1402 | sb->s_root = root_dentry; |
e9be9d5e MS |
1403 | |
1404 | return 0; | |
1405 | ||
3b7a9a24 MS |
1406 | out_free_oe: |
1407 | kfree(oe); | |
3fe6e52f AM |
1408 | out_put_cred: |
1409 | put_cred(ufs->creator_cred); | |
e9be9d5e | 1410 | out_put_lower_mnt: |
dd662667 MS |
1411 | for (i = 0; i < ufs->numlower; i++) |
1412 | mntput(ufs->lower_mnt[i]); | |
1413 | kfree(ufs->lower_mnt); | |
3b7a9a24 MS |
1414 | out_put_workdir: |
1415 | dput(ufs->workdir); | |
e9be9d5e | 1416 | mntput(ufs->upper_mnt); |
e9be9d5e | 1417 | out_put_lowerpath: |
a78d9f0d MS |
1418 | for (i = 0; i < numlower; i++) |
1419 | path_put(&stack[i]); | |
1420 | kfree(stack); | |
1421 | out_free_lowertmp: | |
1422 | kfree(lowertmp); | |
3b7a9a24 MS |
1423 | out_put_workpath: |
1424 | path_put(&workpath); | |
e9be9d5e MS |
1425 | out_put_upperpath: |
1426 | path_put(&upperpath); | |
e9be9d5e | 1427 | out_free_config: |
f45827e8 EZ |
1428 | kfree(ufs->config.lowerdir); |
1429 | kfree(ufs->config.upperdir); | |
1430 | kfree(ufs->config.workdir); | |
1431 | kfree(ufs); | |
e9be9d5e MS |
1432 | out: |
1433 | return err; | |
1434 | } | |
1435 | ||
1436 | static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags, | |
1437 | const char *dev_name, void *raw_data) | |
1438 | { | |
1439 | return mount_nodev(fs_type, flags, raw_data, ovl_fill_super); | |
1440 | } | |
1441 | ||
1442 | static struct file_system_type ovl_fs_type = { | |
1443 | .owner = THIS_MODULE, | |
ef94b186 | 1444 | .name = "overlay", |
e9be9d5e MS |
1445 | .mount = ovl_mount, |
1446 | .kill_sb = kill_anon_super, | |
1447 | }; | |
ef94b186 | 1448 | MODULE_ALIAS_FS("overlay"); |
e9be9d5e MS |
1449 | |
1450 | static int __init ovl_init(void) | |
1451 | { | |
1452 | return register_filesystem(&ovl_fs_type); | |
1453 | } | |
1454 | ||
1455 | static void __exit ovl_exit(void) | |
1456 | { | |
1457 | unregister_filesystem(&ovl_fs_type); | |
1458 | } | |
1459 | ||
1460 | module_init(ovl_init); | |
1461 | module_exit(ovl_exit); |