xref: /openbmc/linux/fs/overlayfs/util.c (revision 5927145e)
1 /*
2  * Copyright (C) 2011 Novell Inc.
3  * Copyright (C) 2016 Red Hat, 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/mount.h>
12 #include <linux/slab.h>
13 #include <linux/cred.h>
14 #include <linux/xattr.h>
15 #include <linux/exportfs.h>
16 #include <linux/uuid.h>
17 #include <linux/namei.h>
18 #include <linux/ratelimit.h>
19 #include "overlayfs.h"
20 
21 int ovl_want_write(struct dentry *dentry)
22 {
23 	struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
24 	return mnt_want_write(ofs->upper_mnt);
25 }
26 
27 void ovl_drop_write(struct dentry *dentry)
28 {
29 	struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
30 	mnt_drop_write(ofs->upper_mnt);
31 }
32 
33 struct dentry *ovl_workdir(struct dentry *dentry)
34 {
35 	struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
36 	return ofs->workdir;
37 }
38 
39 const struct cred *ovl_override_creds(struct super_block *sb)
40 {
41 	struct ovl_fs *ofs = sb->s_fs_info;
42 
43 	return override_creds(ofs->creator_cred);
44 }
45 
46 struct super_block *ovl_same_sb(struct super_block *sb)
47 {
48 	struct ovl_fs *ofs = sb->s_fs_info;
49 
50 	return ofs->same_sb;
51 }
52 
53 bool ovl_can_decode_fh(struct super_block *sb)
54 {
55 	return (sb->s_export_op && sb->s_export_op->fh_to_dentry &&
56 		!uuid_is_null(&sb->s_uuid));
57 }
58 
59 struct dentry *ovl_indexdir(struct super_block *sb)
60 {
61 	struct ovl_fs *ofs = sb->s_fs_info;
62 
63 	return ofs->indexdir;
64 }
65 
66 /* Index all files on copy up. For now only enabled for NFS export */
67 bool ovl_index_all(struct super_block *sb)
68 {
69 	struct ovl_fs *ofs = sb->s_fs_info;
70 
71 	return ofs->config.nfs_export && ofs->config.index;
72 }
73 
74 /* Verify lower origin on lookup. For now only enabled for NFS export */
75 bool ovl_verify_lower(struct super_block *sb)
76 {
77 	struct ovl_fs *ofs = sb->s_fs_info;
78 
79 	return ofs->config.nfs_export && ofs->config.index;
80 }
81 
82 struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
83 {
84 	size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
85 	struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
86 
87 	if (oe)
88 		oe->numlower = numlower;
89 
90 	return oe;
91 }
92 
93 bool ovl_dentry_remote(struct dentry *dentry)
94 {
95 	return dentry->d_flags &
96 		(DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE |
97 		 DCACHE_OP_REAL);
98 }
99 
100 bool ovl_dentry_weird(struct dentry *dentry)
101 {
102 	return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
103 				  DCACHE_MANAGE_TRANSIT |
104 				  DCACHE_OP_HASH |
105 				  DCACHE_OP_COMPARE);
106 }
107 
108 enum ovl_path_type ovl_path_type(struct dentry *dentry)
109 {
110 	struct ovl_entry *oe = dentry->d_fsdata;
111 	enum ovl_path_type type = 0;
112 
113 	if (ovl_dentry_upper(dentry)) {
114 		type = __OVL_PATH_UPPER;
115 
116 		/*
117 		 * Non-dir dentry can hold lower dentry of its copy up origin.
118 		 */
119 		if (oe->numlower) {
120 			type |= __OVL_PATH_ORIGIN;
121 			if (d_is_dir(dentry))
122 				type |= __OVL_PATH_MERGE;
123 		}
124 	} else {
125 		if (oe->numlower > 1)
126 			type |= __OVL_PATH_MERGE;
127 	}
128 	return type;
129 }
130 
131 void ovl_path_upper(struct dentry *dentry, struct path *path)
132 {
133 	struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
134 
135 	path->mnt = ofs->upper_mnt;
136 	path->dentry = ovl_dentry_upper(dentry);
137 }
138 
139 void ovl_path_lower(struct dentry *dentry, struct path *path)
140 {
141 	struct ovl_entry *oe = dentry->d_fsdata;
142 
143 	if (oe->numlower) {
144 		path->mnt = oe->lowerstack[0].layer->mnt;
145 		path->dentry = oe->lowerstack[0].dentry;
146 	} else {
147 		*path = (struct path) { };
148 	}
149 }
150 
151 enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
152 {
153 	enum ovl_path_type type = ovl_path_type(dentry);
154 
155 	if (!OVL_TYPE_UPPER(type))
156 		ovl_path_lower(dentry, path);
157 	else
158 		ovl_path_upper(dentry, path);
159 
160 	return type;
161 }
162 
163 struct dentry *ovl_dentry_upper(struct dentry *dentry)
164 {
165 	return ovl_upperdentry_dereference(OVL_I(d_inode(dentry)));
166 }
167 
168 struct dentry *ovl_dentry_lower(struct dentry *dentry)
169 {
170 	struct ovl_entry *oe = dentry->d_fsdata;
171 
172 	return oe->numlower ? oe->lowerstack[0].dentry : NULL;
173 }
174 
175 struct dentry *ovl_dentry_real(struct dentry *dentry)
176 {
177 	return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry);
178 }
179 
180 struct dentry *ovl_i_dentry_upper(struct inode *inode)
181 {
182 	return ovl_upperdentry_dereference(OVL_I(inode));
183 }
184 
185 struct inode *ovl_inode_upper(struct inode *inode)
186 {
187 	struct dentry *upperdentry = ovl_i_dentry_upper(inode);
188 
189 	return upperdentry ? d_inode(upperdentry) : NULL;
190 }
191 
192 struct inode *ovl_inode_lower(struct inode *inode)
193 {
194 	return OVL_I(inode)->lower;
195 }
196 
197 struct inode *ovl_inode_real(struct inode *inode)
198 {
199 	return ovl_inode_upper(inode) ?: ovl_inode_lower(inode);
200 }
201 
202 
203 struct ovl_dir_cache *ovl_dir_cache(struct inode *inode)
204 {
205 	return OVL_I(inode)->cache;
206 }
207 
208 void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache)
209 {
210 	OVL_I(inode)->cache = cache;
211 }
212 
213 void ovl_dentry_set_flag(unsigned long flag, struct dentry *dentry)
214 {
215 	set_bit(flag, &OVL_E(dentry)->flags);
216 }
217 
218 void ovl_dentry_clear_flag(unsigned long flag, struct dentry *dentry)
219 {
220 	clear_bit(flag, &OVL_E(dentry)->flags);
221 }
222 
223 bool ovl_dentry_test_flag(unsigned long flag, struct dentry *dentry)
224 {
225 	return test_bit(flag, &OVL_E(dentry)->flags);
226 }
227 
228 bool ovl_dentry_is_opaque(struct dentry *dentry)
229 {
230 	return ovl_dentry_test_flag(OVL_E_OPAQUE, dentry);
231 }
232 
233 bool ovl_dentry_is_whiteout(struct dentry *dentry)
234 {
235 	return !dentry->d_inode && ovl_dentry_is_opaque(dentry);
236 }
237 
238 void ovl_dentry_set_opaque(struct dentry *dentry)
239 {
240 	ovl_dentry_set_flag(OVL_E_OPAQUE, dentry);
241 }
242 
243 /*
244  * For hard links and decoded file handles, it's possible for ovl_dentry_upper()
245  * to return positive, while there's no actual upper alias for the inode.
246  * Copy up code needs to know about the existence of the upper alias, so it
247  * can't use ovl_dentry_upper().
248  */
249 bool ovl_dentry_has_upper_alias(struct dentry *dentry)
250 {
251 	return ovl_dentry_test_flag(OVL_E_UPPER_ALIAS, dentry);
252 }
253 
254 void ovl_dentry_set_upper_alias(struct dentry *dentry)
255 {
256 	ovl_dentry_set_flag(OVL_E_UPPER_ALIAS, dentry);
257 }
258 
259 bool ovl_redirect_dir(struct super_block *sb)
260 {
261 	struct ovl_fs *ofs = sb->s_fs_info;
262 
263 	return ofs->config.redirect_dir && !ofs->noxattr;
264 }
265 
266 const char *ovl_dentry_get_redirect(struct dentry *dentry)
267 {
268 	return OVL_I(d_inode(dentry))->redirect;
269 }
270 
271 void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect)
272 {
273 	struct ovl_inode *oi = OVL_I(d_inode(dentry));
274 
275 	kfree(oi->redirect);
276 	oi->redirect = redirect;
277 }
278 
279 void ovl_inode_init(struct inode *inode, struct dentry *upperdentry,
280 		    struct dentry *lowerdentry)
281 {
282 	if (upperdentry)
283 		OVL_I(inode)->__upperdentry = upperdentry;
284 	if (lowerdentry)
285 		OVL_I(inode)->lower = igrab(d_inode(lowerdentry));
286 
287 	ovl_copyattr(d_inode(upperdentry ?: lowerdentry), inode);
288 }
289 
290 void ovl_inode_update(struct inode *inode, struct dentry *upperdentry)
291 {
292 	struct inode *upperinode = d_inode(upperdentry);
293 
294 	WARN_ON(OVL_I(inode)->__upperdentry);
295 
296 	/*
297 	 * Make sure upperdentry is consistent before making it visible
298 	 */
299 	smp_wmb();
300 	OVL_I(inode)->__upperdentry = upperdentry;
301 	if (inode_unhashed(inode)) {
302 		inode->i_private = upperinode;
303 		__insert_inode_hash(inode, (unsigned long) upperinode);
304 	}
305 }
306 
307 void ovl_dentry_version_inc(struct dentry *dentry, bool impurity)
308 {
309 	struct inode *inode = d_inode(dentry);
310 
311 	WARN_ON(!inode_is_locked(inode));
312 	/*
313 	 * Version is used by readdir code to keep cache consistent.  For merge
314 	 * dirs all changes need to be noted.  For non-merge dirs, cache only
315 	 * contains impure (ones which have been copied up and have origins)
316 	 * entries, so only need to note changes to impure entries.
317 	 */
318 	if (OVL_TYPE_MERGE(ovl_path_type(dentry)) || impurity)
319 		OVL_I(inode)->version++;
320 }
321 
322 u64 ovl_dentry_version_get(struct dentry *dentry)
323 {
324 	struct inode *inode = d_inode(dentry);
325 
326 	WARN_ON(!inode_is_locked(inode));
327 	return OVL_I(inode)->version;
328 }
329 
330 bool ovl_is_whiteout(struct dentry *dentry)
331 {
332 	struct inode *inode = dentry->d_inode;
333 
334 	return inode && IS_WHITEOUT(inode);
335 }
336 
337 struct file *ovl_path_open(struct path *path, int flags)
338 {
339 	return dentry_open(path, flags | O_NOATIME, current_cred());
340 }
341 
342 int ovl_copy_up_start(struct dentry *dentry)
343 {
344 	struct ovl_inode *oi = OVL_I(d_inode(dentry));
345 	int err;
346 
347 	err = mutex_lock_interruptible(&oi->lock);
348 	if (!err && ovl_dentry_has_upper_alias(dentry)) {
349 		err = 1; /* Already copied up */
350 		mutex_unlock(&oi->lock);
351 	}
352 
353 	return err;
354 }
355 
356 void ovl_copy_up_end(struct dentry *dentry)
357 {
358 	mutex_unlock(&OVL_I(d_inode(dentry))->lock);
359 }
360 
361 bool ovl_check_origin_xattr(struct dentry *dentry)
362 {
363 	int res;
364 
365 	res = vfs_getxattr(dentry, OVL_XATTR_ORIGIN, NULL, 0);
366 
367 	/* Zero size value means "copied up but origin unknown" */
368 	if (res >= 0)
369 		return true;
370 
371 	return false;
372 }
373 
374 bool ovl_check_dir_xattr(struct dentry *dentry, const char *name)
375 {
376 	int res;
377 	char val;
378 
379 	if (!d_is_dir(dentry))
380 		return false;
381 
382 	res = vfs_getxattr(dentry, name, &val, 1);
383 	if (res == 1 && val == 'y')
384 		return true;
385 
386 	return false;
387 }
388 
389 int ovl_check_setxattr(struct dentry *dentry, struct dentry *upperdentry,
390 		       const char *name, const void *value, size_t size,
391 		       int xerr)
392 {
393 	int err;
394 	struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
395 
396 	if (ofs->noxattr)
397 		return xerr;
398 
399 	err = ovl_do_setxattr(upperdentry, name, value, size, 0);
400 
401 	if (err == -EOPNOTSUPP) {
402 		pr_warn("overlayfs: cannot set %s xattr on upper\n", name);
403 		ofs->noxattr = true;
404 		return xerr;
405 	}
406 
407 	return err;
408 }
409 
410 int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry)
411 {
412 	int err;
413 
414 	if (ovl_test_flag(OVL_IMPURE, d_inode(dentry)))
415 		return 0;
416 
417 	/*
418 	 * Do not fail when upper doesn't support xattrs.
419 	 * Upper inodes won't have origin nor redirect xattr anyway.
420 	 */
421 	err = ovl_check_setxattr(dentry, upperdentry, OVL_XATTR_IMPURE,
422 				 "y", 1, 0);
423 	if (!err)
424 		ovl_set_flag(OVL_IMPURE, d_inode(dentry));
425 
426 	return err;
427 }
428 
429 void ovl_set_flag(unsigned long flag, struct inode *inode)
430 {
431 	set_bit(flag, &OVL_I(inode)->flags);
432 }
433 
434 void ovl_clear_flag(unsigned long flag, struct inode *inode)
435 {
436 	clear_bit(flag, &OVL_I(inode)->flags);
437 }
438 
439 bool ovl_test_flag(unsigned long flag, struct inode *inode)
440 {
441 	return test_bit(flag, &OVL_I(inode)->flags);
442 }
443 
444 /**
445  * Caller must hold a reference to inode to prevent it from being freed while
446  * it is marked inuse.
447  */
448 bool ovl_inuse_trylock(struct dentry *dentry)
449 {
450 	struct inode *inode = d_inode(dentry);
451 	bool locked = false;
452 
453 	spin_lock(&inode->i_lock);
454 	if (!(inode->i_state & I_OVL_INUSE)) {
455 		inode->i_state |= I_OVL_INUSE;
456 		locked = true;
457 	}
458 	spin_unlock(&inode->i_lock);
459 
460 	return locked;
461 }
462 
463 void ovl_inuse_unlock(struct dentry *dentry)
464 {
465 	if (dentry) {
466 		struct inode *inode = d_inode(dentry);
467 
468 		spin_lock(&inode->i_lock);
469 		WARN_ON(!(inode->i_state & I_OVL_INUSE));
470 		inode->i_state &= ~I_OVL_INUSE;
471 		spin_unlock(&inode->i_lock);
472 	}
473 }
474 
475 /*
476  * Does this overlay dentry need to be indexed on copy up?
477  */
478 bool ovl_need_index(struct dentry *dentry)
479 {
480 	struct dentry *lower = ovl_dentry_lower(dentry);
481 
482 	if (!lower || !ovl_indexdir(dentry->d_sb))
483 		return false;
484 
485 	/* Index all files for NFS export and consistency verification */
486 	if (ovl_index_all(dentry->d_sb))
487 		return true;
488 
489 	/* Index only lower hardlinks on copy up */
490 	if (!d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
491 		return true;
492 
493 	return false;
494 }
495 
496 /* Caller must hold OVL_I(inode)->lock */
497 static void ovl_cleanup_index(struct dentry *dentry)
498 {
499 	struct dentry *indexdir = ovl_indexdir(dentry->d_sb);
500 	struct inode *dir = indexdir->d_inode;
501 	struct dentry *lowerdentry = ovl_dentry_lower(dentry);
502 	struct dentry *upperdentry = ovl_dentry_upper(dentry);
503 	struct dentry *index = NULL;
504 	struct inode *inode;
505 	struct qstr name;
506 	int err;
507 
508 	err = ovl_get_index_name(lowerdentry, &name);
509 	if (err)
510 		goto fail;
511 
512 	inode = d_inode(upperdentry);
513 	if (!S_ISDIR(inode->i_mode) && inode->i_nlink != 1) {
514 		pr_warn_ratelimited("overlayfs: cleanup linked index (%pd2, ino=%lu, nlink=%u)\n",
515 				    upperdentry, inode->i_ino, inode->i_nlink);
516 		/*
517 		 * We either have a bug with persistent union nlink or a lower
518 		 * hardlink was added while overlay is mounted. Adding a lower
519 		 * hardlink and then unlinking all overlay hardlinks would drop
520 		 * overlay nlink to zero before all upper inodes are unlinked.
521 		 * As a safety measure, when that situation is detected, set
522 		 * the overlay nlink to the index inode nlink minus one for the
523 		 * index entry itself.
524 		 */
525 		set_nlink(d_inode(dentry), inode->i_nlink - 1);
526 		ovl_set_nlink_upper(dentry);
527 		goto out;
528 	}
529 
530 	inode_lock_nested(dir, I_MUTEX_PARENT);
531 	index = lookup_one_len(name.name, indexdir, name.len);
532 	err = PTR_ERR(index);
533 	if (IS_ERR(index)) {
534 		index = NULL;
535 	} else if (ovl_index_all(dentry->d_sb)) {
536 		/* Whiteout orphan index to block future open by handle */
537 		err = ovl_cleanup_and_whiteout(indexdir, dir, index);
538 	} else {
539 		/* Cleanup orphan index entries */
540 		err = ovl_cleanup(dir, index);
541 	}
542 
543 	inode_unlock(dir);
544 	if (err)
545 		goto fail;
546 
547 out:
548 	dput(index);
549 	return;
550 
551 fail:
552 	pr_err("overlayfs: cleanup index of '%pd2' failed (%i)\n", dentry, err);
553 	goto out;
554 }
555 
556 /*
557  * Operations that change overlay inode and upper inode nlink need to be
558  * synchronized with copy up for persistent nlink accounting.
559  */
560 int ovl_nlink_start(struct dentry *dentry, bool *locked)
561 {
562 	struct ovl_inode *oi = OVL_I(d_inode(dentry));
563 	const struct cred *old_cred;
564 	int err;
565 
566 	if (!d_inode(dentry))
567 		return 0;
568 
569 	/*
570 	 * With inodes index is enabled, we store the union overlay nlink
571 	 * in an xattr on the index inode. When whiting out an indexed lower,
572 	 * we need to decrement the overlay persistent nlink, but before the
573 	 * first copy up, we have no upper index inode to store the xattr.
574 	 *
575 	 * As a workaround, before whiteout/rename over an indexed lower,
576 	 * copy up to create the upper index. Creating the upper index will
577 	 * initialize the overlay nlink, so it could be dropped if unlink
578 	 * or rename succeeds.
579 	 *
580 	 * TODO: implement metadata only index copy up when called with
581 	 *       ovl_copy_up_flags(dentry, O_PATH).
582 	 */
583 	if (ovl_need_index(dentry) && !ovl_dentry_has_upper_alias(dentry)) {
584 		err = ovl_copy_up(dentry);
585 		if (err)
586 			return err;
587 	}
588 
589 	err = mutex_lock_interruptible(&oi->lock);
590 	if (err)
591 		return err;
592 
593 	if (d_is_dir(dentry) || !ovl_test_flag(OVL_INDEX, d_inode(dentry)))
594 		goto out;
595 
596 	old_cred = ovl_override_creds(dentry->d_sb);
597 	/*
598 	 * The overlay inode nlink should be incremented/decremented IFF the
599 	 * upper operation succeeds, along with nlink change of upper inode.
600 	 * Therefore, before link/unlink/rename, we store the union nlink
601 	 * value relative to the upper inode nlink in an upper inode xattr.
602 	 */
603 	err = ovl_set_nlink_upper(dentry);
604 	revert_creds(old_cred);
605 
606 out:
607 	if (err)
608 		mutex_unlock(&oi->lock);
609 	else
610 		*locked = true;
611 
612 	return err;
613 }
614 
615 void ovl_nlink_end(struct dentry *dentry, bool locked)
616 {
617 	if (locked) {
618 		if (ovl_test_flag(OVL_INDEX, d_inode(dentry)) &&
619 		    d_inode(dentry)->i_nlink == 0) {
620 			const struct cred *old_cred;
621 
622 			old_cred = ovl_override_creds(dentry->d_sb);
623 			ovl_cleanup_index(dentry);
624 			revert_creds(old_cred);
625 		}
626 
627 		mutex_unlock(&OVL_I(d_inode(dentry))->lock);
628 	}
629 }
630 
631 int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir)
632 {
633 	/* Workdir should not be the same as upperdir */
634 	if (workdir == upperdir)
635 		goto err;
636 
637 	/* Workdir should not be subdir of upperdir and vice versa */
638 	if (lock_rename(workdir, upperdir) != NULL)
639 		goto err_unlock;
640 
641 	return 0;
642 
643 err_unlock:
644 	unlock_rename(workdir, upperdir);
645 err:
646 	pr_err("overlayfs: failed to lock workdir+upperdir\n");
647 	return -EIO;
648 }
649