1
2
3
4
5
6
7
8
9
10#include <linux/fs.h>
11#include <linux/mount.h>
12#include <linux/slab.h>
13#include <linux/xattr.h>
14#include <linux/cred.h>
15#include <linux/sched.h>
16#include <linux/exportfs.h>
17#include <linux/uuid.h>
18#include <linux/namei.h>
19#include <linux/ratelimit.h>
20#include "overlayfs.h"
21
22int ovl_want_write(struct dentry *dentry)
23{
24 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
25 return mnt_want_write(ofs->upper_mnt);
26}
27
28void ovl_drop_write(struct dentry *dentry)
29{
30 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
31 mnt_drop_write(ofs->upper_mnt);
32}
33
34struct dentry *ovl_workdir(struct dentry *dentry)
35{
36 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
37 return ofs->workdir;
38}
39
40const struct cred *ovl_override_creds(struct super_block *sb)
41{
42 struct ovl_fs *ofs = sb->s_fs_info;
43
44 return override_creds(ofs->creator_cred);
45}
46
47struct super_block *ovl_same_sb(struct super_block *sb)
48{
49 struct ovl_fs *ofs = sb->s_fs_info;
50
51 if (!ofs->numlowerfs)
52 return ofs->upper_mnt->mnt_sb;
53 else if (ofs->numlowerfs == 1 && !ofs->upper_mnt)
54 return ofs->lower_fs[0].sb;
55 else
56 return NULL;
57}
58
59
60
61
62
63
64
65
66
67int ovl_can_decode_fh(struct super_block *sb)
68{
69 if (!sb->s_export_op || !sb->s_export_op->fh_to_dentry ||
70 uuid_is_null((uuid_t *) &sb->s_uuid))
71 return 0;
72
73 return sb->s_export_op->encode_fh ? -1 : FILEID_INO32_GEN;
74}
75
76struct dentry *ovl_indexdir(struct super_block *sb)
77{
78 struct ovl_fs *ofs = sb->s_fs_info;
79
80 return ofs->indexdir;
81}
82
83
84bool ovl_index_all(struct super_block *sb)
85{
86 struct ovl_fs *ofs = sb->s_fs_info;
87
88 return ofs->config.nfs_export && ofs->config.index;
89}
90
91
92bool ovl_verify_lower(struct super_block *sb)
93{
94 struct ovl_fs *ofs = sb->s_fs_info;
95
96 return ofs->config.nfs_export && ofs->config.index;
97}
98
99struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
100{
101 size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
102 struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
103
104 if (oe)
105 oe->numlower = numlower;
106
107 return oe;
108}
109
110bool ovl_dentry_remote(struct dentry *dentry)
111{
112 return dentry->d_flags &
113 (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE |
114 DCACHE_OP_REAL);
115}
116
117bool ovl_dentry_weird(struct dentry *dentry)
118{
119 return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
120 DCACHE_MANAGE_TRANSIT |
121 DCACHE_OP_HASH |
122 DCACHE_OP_COMPARE);
123}
124
125enum ovl_path_type ovl_path_type(struct dentry *dentry)
126{
127 struct ovl_entry *oe = dentry->d_fsdata;
128 enum ovl_path_type type = 0;
129
130 if (ovl_dentry_upper(dentry)) {
131 type = __OVL_PATH_UPPER;
132
133
134
135
136 if (oe->numlower) {
137 type |= __OVL_PATH_ORIGIN;
138 if (d_is_dir(dentry))
139 type |= __OVL_PATH_MERGE;
140 }
141 } else {
142 if (oe->numlower > 1)
143 type |= __OVL_PATH_MERGE;
144 }
145 return type;
146}
147
148void ovl_path_upper(struct dentry *dentry, struct path *path)
149{
150 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
151
152 path->mnt = ofs->upper_mnt;
153 path->dentry = ovl_dentry_upper(dentry);
154}
155
156void ovl_path_lower(struct dentry *dentry, struct path *path)
157{
158 struct ovl_entry *oe = dentry->d_fsdata;
159
160 if (oe->numlower) {
161 path->mnt = oe->lowerstack[0].layer->mnt;
162 path->dentry = oe->lowerstack[0].dentry;
163 } else {
164 *path = (struct path) { };
165 }
166}
167
168enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
169{
170 enum ovl_path_type type = ovl_path_type(dentry);
171
172 if (!OVL_TYPE_UPPER(type))
173 ovl_path_lower(dentry, path);
174 else
175 ovl_path_upper(dentry, path);
176
177 return type;
178}
179
180struct dentry *ovl_dentry_upper(struct dentry *dentry)
181{
182 return ovl_upperdentry_dereference(OVL_I(d_inode(dentry)));
183}
184
185struct dentry *ovl_dentry_lower(struct dentry *dentry)
186{
187 struct ovl_entry *oe = dentry->d_fsdata;
188
189 return oe->numlower ? oe->lowerstack[0].dentry : NULL;
190}
191
192struct ovl_layer *ovl_layer_lower(struct dentry *dentry)
193{
194 struct ovl_entry *oe = dentry->d_fsdata;
195
196 return oe->numlower ? oe->lowerstack[0].layer : NULL;
197}
198
199struct dentry *ovl_dentry_real(struct dentry *dentry)
200{
201 return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry);
202}
203
204struct dentry *ovl_i_dentry_upper(struct inode *inode)
205{
206 return ovl_upperdentry_dereference(OVL_I(inode));
207}
208
209struct inode *ovl_inode_upper(struct inode *inode)
210{
211 struct dentry *upperdentry = ovl_i_dentry_upper(inode);
212
213 return upperdentry ? d_inode(upperdentry) : NULL;
214}
215
216struct inode *ovl_inode_lower(struct inode *inode)
217{
218 return OVL_I(inode)->lower;
219}
220
221struct inode *ovl_inode_real(struct inode *inode)
222{
223 return ovl_inode_upper(inode) ?: ovl_inode_lower(inode);
224}
225
226
227struct ovl_dir_cache *ovl_dir_cache(struct inode *inode)
228{
229 return OVL_I(inode)->cache;
230}
231
232void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache)
233{
234 OVL_I(inode)->cache = cache;
235}
236
237void ovl_dentry_set_flag(unsigned long flag, struct dentry *dentry)
238{
239 set_bit(flag, &OVL_E(dentry)->flags);
240}
241
242void ovl_dentry_clear_flag(unsigned long flag, struct dentry *dentry)
243{
244 clear_bit(flag, &OVL_E(dentry)->flags);
245}
246
247bool ovl_dentry_test_flag(unsigned long flag, struct dentry *dentry)
248{
249 return test_bit(flag, &OVL_E(dentry)->flags);
250}
251
252bool ovl_dentry_is_opaque(struct dentry *dentry)
253{
254 return ovl_dentry_test_flag(OVL_E_OPAQUE, dentry);
255}
256
257bool ovl_dentry_is_whiteout(struct dentry *dentry)
258{
259 return !dentry->d_inode && ovl_dentry_is_opaque(dentry);
260}
261
262void ovl_dentry_set_opaque(struct dentry *dentry)
263{
264 ovl_dentry_set_flag(OVL_E_OPAQUE, dentry);
265}
266
267
268
269
270
271
272
273bool ovl_dentry_has_upper_alias(struct dentry *dentry)
274{
275 return ovl_dentry_test_flag(OVL_E_UPPER_ALIAS, dentry);
276}
277
278void ovl_dentry_set_upper_alias(struct dentry *dentry)
279{
280 ovl_dentry_set_flag(OVL_E_UPPER_ALIAS, dentry);
281}
282
283bool ovl_redirect_dir(struct super_block *sb)
284{
285 struct ovl_fs *ofs = sb->s_fs_info;
286
287 return ofs->config.redirect_dir && !ofs->noxattr;
288}
289
290const char *ovl_dentry_get_redirect(struct dentry *dentry)
291{
292 return OVL_I(d_inode(dentry))->redirect;
293}
294
295void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect)
296{
297 struct ovl_inode *oi = OVL_I(d_inode(dentry));
298
299 kfree(oi->redirect);
300 oi->redirect = redirect;
301}
302
303void ovl_inode_init(struct inode *inode, struct dentry *upperdentry,
304 struct dentry *lowerdentry)
305{
306 struct inode *realinode = d_inode(upperdentry ?: lowerdentry);
307
308 if (upperdentry)
309 OVL_I(inode)->__upperdentry = upperdentry;
310 if (lowerdentry)
311 OVL_I(inode)->lower = igrab(d_inode(lowerdentry));
312
313 ovl_copyattr(realinode, inode);
314 ovl_copyflags(realinode, inode);
315 if (!inode->i_ino)
316 inode->i_ino = realinode->i_ino;
317}
318
319void ovl_inode_update(struct inode *inode, struct dentry *upperdentry)
320{
321 struct inode *upperinode = d_inode(upperdentry);
322
323 WARN_ON(OVL_I(inode)->__upperdentry);
324
325
326
327
328 smp_wmb();
329 OVL_I(inode)->__upperdentry = upperdentry;
330 if (inode_unhashed(inode)) {
331 if (!inode->i_ino)
332 inode->i_ino = upperinode->i_ino;
333 inode->i_private = upperinode;
334 __insert_inode_hash(inode, (unsigned long) upperinode);
335 }
336}
337
338void ovl_dentry_version_inc(struct dentry *dentry, bool impurity)
339{
340 struct inode *inode = d_inode(dentry);
341
342 WARN_ON(!inode_is_locked(inode));
343
344
345
346
347
348
349 if (OVL_TYPE_MERGE(ovl_path_type(dentry)) || impurity)
350 OVL_I(inode)->version++;
351}
352
353u64 ovl_dentry_version_get(struct dentry *dentry)
354{
355 struct inode *inode = d_inode(dentry);
356
357 WARN_ON(!inode_is_locked(inode));
358 return OVL_I(inode)->version;
359}
360
361bool ovl_is_whiteout(struct dentry *dentry)
362{
363 struct inode *inode = dentry->d_inode;
364
365 return inode && IS_WHITEOUT(inode);
366}
367
368struct file *ovl_path_open(struct path *path, int flags)
369{
370 return dentry_open(path, flags | O_NOATIME, current_cred());
371}
372
373int ovl_copy_up_start(struct dentry *dentry)
374{
375 struct ovl_inode *oi = OVL_I(d_inode(dentry));
376 int err;
377
378 err = mutex_lock_interruptible(&oi->lock);
379 if (!err && ovl_dentry_has_upper_alias(dentry)) {
380 err = 1;
381 mutex_unlock(&oi->lock);
382 }
383
384 return err;
385}
386
387void ovl_copy_up_end(struct dentry *dentry)
388{
389 mutex_unlock(&OVL_I(d_inode(dentry))->lock);
390}
391
392bool ovl_check_origin_xattr(struct dentry *dentry)
393{
394 int res;
395
396 res = vfs_getxattr(dentry, OVL_XATTR_ORIGIN, NULL, 0);
397
398
399 if (res >= 0)
400 return true;
401
402 return false;
403}
404
405bool ovl_check_dir_xattr(struct dentry *dentry, const char *name)
406{
407 int res;
408 char val;
409
410 if (!d_is_dir(dentry))
411 return false;
412
413 res = vfs_getxattr(dentry, name, &val, 1);
414 if (res == 1 && val == 'y')
415 return true;
416
417 return false;
418}
419
420int ovl_check_setxattr(struct dentry *dentry, struct dentry *upperdentry,
421 const char *name, const void *value, size_t size,
422 int xerr)
423{
424 int err;
425 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
426
427 if (ofs->noxattr)
428 return xerr;
429
430 err = ovl_do_setxattr(upperdentry, name, value, size, 0);
431
432 if (err == -EOPNOTSUPP) {
433 pr_warn("overlayfs: cannot set %s xattr on upper\n", name);
434 ofs->noxattr = true;
435 return xerr;
436 }
437
438 return err;
439}
440
441int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry)
442{
443 int err;
444
445 if (ovl_test_flag(OVL_IMPURE, d_inode(dentry)))
446 return 0;
447
448
449
450
451
452 err = ovl_check_setxattr(dentry, upperdentry, OVL_XATTR_IMPURE,
453 "y", 1, 0);
454 if (!err)
455 ovl_set_flag(OVL_IMPURE, d_inode(dentry));
456
457 return err;
458}
459
460void ovl_set_flag(unsigned long flag, struct inode *inode)
461{
462 set_bit(flag, &OVL_I(inode)->flags);
463}
464
465void ovl_clear_flag(unsigned long flag, struct inode *inode)
466{
467 clear_bit(flag, &OVL_I(inode)->flags);
468}
469
470bool ovl_test_flag(unsigned long flag, struct inode *inode)
471{
472 return test_bit(flag, &OVL_I(inode)->flags);
473}
474
475
476
477
478
479bool ovl_inuse_trylock(struct dentry *dentry)
480{
481 struct inode *inode = d_inode(dentry);
482 bool locked = false;
483
484 spin_lock(&inode->i_lock);
485 if (!(inode->i_state & I_OVL_INUSE)) {
486 inode->i_state |= I_OVL_INUSE;
487 locked = true;
488 }
489 spin_unlock(&inode->i_lock);
490
491 return locked;
492}
493
494void ovl_inuse_unlock(struct dentry *dentry)
495{
496 if (dentry) {
497 struct inode *inode = d_inode(dentry);
498
499 spin_lock(&inode->i_lock);
500 WARN_ON(!(inode->i_state & I_OVL_INUSE));
501 inode->i_state &= ~I_OVL_INUSE;
502 spin_unlock(&inode->i_lock);
503 }
504}
505
506
507
508
509bool ovl_need_index(struct dentry *dentry)
510{
511 struct dentry *lower = ovl_dentry_lower(dentry);
512
513 if (!lower || !ovl_indexdir(dentry->d_sb))
514 return false;
515
516
517 if (ovl_index_all(dentry->d_sb))
518 return true;
519
520
521 if (!d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
522 return true;
523
524 return false;
525}
526
527
528static void ovl_cleanup_index(struct dentry *dentry)
529{
530 struct dentry *indexdir = ovl_indexdir(dentry->d_sb);
531 struct inode *dir = indexdir->d_inode;
532 struct dentry *lowerdentry = ovl_dentry_lower(dentry);
533 struct dentry *upperdentry = ovl_dentry_upper(dentry);
534 struct dentry *index = NULL;
535 struct inode *inode;
536 struct qstr name = { };
537 int err;
538
539 err = ovl_get_index_name(lowerdentry, &name);
540 if (err)
541 goto fail;
542
543 inode = d_inode(upperdentry);
544 if (!S_ISDIR(inode->i_mode) && inode->i_nlink != 1) {
545 pr_warn_ratelimited("overlayfs: cleanup linked index (%pd2, ino=%lu, nlink=%u)\n",
546 upperdentry, inode->i_ino, inode->i_nlink);
547
548
549
550
551
552
553
554
555
556 set_nlink(d_inode(dentry), inode->i_nlink - 1);
557 ovl_set_nlink_upper(dentry);
558 goto out;
559 }
560
561 inode_lock_nested(dir, I_MUTEX_PARENT);
562 index = lookup_one_len(name.name, indexdir, name.len);
563 err = PTR_ERR(index);
564 if (IS_ERR(index)) {
565 index = NULL;
566 } else if (ovl_index_all(dentry->d_sb)) {
567
568 err = ovl_cleanup_and_whiteout(indexdir, dir, index);
569 } else {
570
571 err = ovl_cleanup(dir, index);
572 }
573
574 inode_unlock(dir);
575 if (err)
576 goto fail;
577
578out:
579 kfree(name.name);
580 dput(index);
581 return;
582
583fail:
584 pr_err("overlayfs: cleanup index of '%pd2' failed (%i)\n", dentry, err);
585 goto out;
586}
587
588
589
590
591
592int ovl_nlink_start(struct dentry *dentry, bool *locked)
593{
594 struct ovl_inode *oi = OVL_I(d_inode(dentry));
595 const struct cred *old_cred;
596 int err;
597
598 if (!d_inode(dentry))
599 return 0;
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615 if (ovl_need_index(dentry) && !ovl_dentry_has_upper_alias(dentry)) {
616 err = ovl_copy_up(dentry);
617 if (err)
618 return err;
619 }
620
621 err = mutex_lock_interruptible(&oi->lock);
622 if (err)
623 return err;
624
625 if (d_is_dir(dentry) || !ovl_test_flag(OVL_INDEX, d_inode(dentry)))
626 goto out;
627
628 old_cred = ovl_override_creds(dentry->d_sb);
629
630
631
632
633
634
635 err = ovl_set_nlink_upper(dentry);
636 revert_creds(old_cred);
637
638out:
639 if (err)
640 mutex_unlock(&oi->lock);
641 else
642 *locked = true;
643
644 return err;
645}
646
647void ovl_nlink_end(struct dentry *dentry, bool locked)
648{
649 if (locked) {
650 if (ovl_test_flag(OVL_INDEX, d_inode(dentry)) &&
651 d_inode(dentry)->i_nlink == 0) {
652 const struct cred *old_cred;
653
654 old_cred = ovl_override_creds(dentry->d_sb);
655 ovl_cleanup_index(dentry);
656 revert_creds(old_cred);
657 }
658
659 mutex_unlock(&OVL_I(d_inode(dentry))->lock);
660 }
661}
662
663int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir)
664{
665
666 if (workdir == upperdir)
667 goto err;
668
669
670 if (lock_rename(workdir, upperdir) != NULL)
671 goto err_unlock;
672
673 return 0;
674
675err_unlock:
676 unlock_rename(workdir, upperdir);
677err:
678 pr_err("overlayfs: failed to lock workdir+upperdir\n");
679 return -EIO;
680}
681