xref: /openbmc/linux/fs/overlayfs/dir.c (revision faffb083)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *
4  * Copyright (C) 2011 Novell Inc.
5  */
6 
7 #include <linux/fs.h>
8 #include <linux/namei.h>
9 #include <linux/xattr.h>
10 #include <linux/security.h>
11 #include <linux/cred.h>
12 #include <linux/module.h>
13 #include <linux/posix_acl.h>
14 #include <linux/posix_acl_xattr.h>
15 #include <linux/atomic.h>
16 #include <linux/ratelimit.h>
17 #include "overlayfs.h"
18 
19 static unsigned short ovl_redirect_max = 256;
20 module_param_named(redirect_max, ovl_redirect_max, ushort, 0644);
21 MODULE_PARM_DESC(redirect_max,
22 		 "Maximum length of absolute redirect xattr value");
23 
24 static int ovl_set_redirect(struct dentry *dentry, bool samedir);
25 
26 int ovl_cleanup(struct ovl_fs *ofs, struct inode *wdir, struct dentry *wdentry)
27 {
28 	int err;
29 
30 	dget(wdentry);
31 	if (d_is_dir(wdentry))
32 		err = ovl_do_rmdir(ofs, wdir, wdentry);
33 	else
34 		err = ovl_do_unlink(ofs, wdir, wdentry);
35 	dput(wdentry);
36 
37 	if (err) {
38 		pr_err("cleanup of '%pd2' failed (%i)\n",
39 		       wdentry, err);
40 	}
41 
42 	return err;
43 }
44 
45 struct dentry *ovl_lookup_temp(struct ovl_fs *ofs, struct dentry *workdir)
46 {
47 	struct dentry *temp;
48 	char name[20];
49 	static atomic_t temp_id = ATOMIC_INIT(0);
50 
51 	/* counter is allowed to wrap, since temp dentries are ephemeral */
52 	snprintf(name, sizeof(name), "#%x", atomic_inc_return(&temp_id));
53 
54 	temp = ovl_lookup_upper(ofs, name, workdir, strlen(name));
55 	if (!IS_ERR(temp) && temp->d_inode) {
56 		pr_err("workdir/%s already exists\n", name);
57 		dput(temp);
58 		temp = ERR_PTR(-EIO);
59 	}
60 
61 	return temp;
62 }
63 
64 /* caller holds i_mutex on workdir */
65 static struct dentry *ovl_whiteout(struct ovl_fs *ofs)
66 {
67 	int err;
68 	struct dentry *whiteout;
69 	struct dentry *workdir = ofs->workdir;
70 	struct inode *wdir = workdir->d_inode;
71 
72 	if (!ofs->whiteout) {
73 		whiteout = ovl_lookup_temp(ofs, workdir);
74 		if (IS_ERR(whiteout))
75 			goto out;
76 
77 		err = ovl_do_whiteout(ofs, wdir, whiteout);
78 		if (err) {
79 			dput(whiteout);
80 			whiteout = ERR_PTR(err);
81 			goto out;
82 		}
83 		ofs->whiteout = whiteout;
84 	}
85 
86 	if (ofs->share_whiteout) {
87 		whiteout = ovl_lookup_temp(ofs, workdir);
88 		if (IS_ERR(whiteout))
89 			goto out;
90 
91 		err = ovl_do_link(ofs, ofs->whiteout, wdir, whiteout);
92 		if (!err)
93 			goto out;
94 
95 		if (err != -EMLINK) {
96 			pr_warn("Failed to link whiteout - disabling whiteout inode sharing(nlink=%u, err=%i)\n",
97 				ofs->whiteout->d_inode->i_nlink, err);
98 			ofs->share_whiteout = false;
99 		}
100 		dput(whiteout);
101 	}
102 	whiteout = ofs->whiteout;
103 	ofs->whiteout = NULL;
104 out:
105 	return whiteout;
106 }
107 
108 /* Caller must hold i_mutex on both workdir and dir */
109 int ovl_cleanup_and_whiteout(struct ovl_fs *ofs, struct inode *dir,
110 			     struct dentry *dentry)
111 {
112 	struct inode *wdir = ofs->workdir->d_inode;
113 	struct dentry *whiteout;
114 	int err;
115 	int flags = 0;
116 
117 	whiteout = ovl_whiteout(ofs);
118 	err = PTR_ERR(whiteout);
119 	if (IS_ERR(whiteout))
120 		return err;
121 
122 	if (d_is_dir(dentry))
123 		flags = RENAME_EXCHANGE;
124 
125 	err = ovl_do_rename(ofs, wdir, whiteout, dir, dentry, flags);
126 	if (err)
127 		goto kill_whiteout;
128 	if (flags)
129 		ovl_cleanup(ofs, wdir, dentry);
130 
131 out:
132 	dput(whiteout);
133 	return err;
134 
135 kill_whiteout:
136 	ovl_cleanup(ofs, wdir, whiteout);
137 	goto out;
138 }
139 
140 int ovl_mkdir_real(struct ovl_fs *ofs, struct inode *dir,
141 		   struct dentry **newdentry, umode_t mode)
142 {
143 	int err;
144 	struct dentry *d, *dentry = *newdentry;
145 
146 	err = ovl_do_mkdir(ofs, dir, dentry, mode);
147 	if (err)
148 		return err;
149 
150 	if (likely(!d_unhashed(dentry)))
151 		return 0;
152 
153 	/*
154 	 * vfs_mkdir() may succeed and leave the dentry passed
155 	 * to it unhashed and negative. If that happens, try to
156 	 * lookup a new hashed and positive dentry.
157 	 */
158 	d = ovl_lookup_upper(ofs, dentry->d_name.name, dentry->d_parent,
159 			     dentry->d_name.len);
160 	if (IS_ERR(d)) {
161 		pr_warn("failed lookup after mkdir (%pd2, err=%i).\n",
162 			dentry, err);
163 		return PTR_ERR(d);
164 	}
165 	dput(dentry);
166 	*newdentry = d;
167 
168 	return 0;
169 }
170 
171 struct dentry *ovl_create_real(struct ovl_fs *ofs, struct inode *dir,
172 			       struct dentry *newdentry, struct ovl_cattr *attr)
173 {
174 	int err;
175 
176 	if (IS_ERR(newdentry))
177 		return newdentry;
178 
179 	err = -ESTALE;
180 	if (newdentry->d_inode)
181 		goto out;
182 
183 	if (attr->hardlink) {
184 		err = ovl_do_link(ofs, attr->hardlink, dir, newdentry);
185 	} else {
186 		switch (attr->mode & S_IFMT) {
187 		case S_IFREG:
188 			err = ovl_do_create(ofs, dir, newdentry, attr->mode);
189 			break;
190 
191 		case S_IFDIR:
192 			/* mkdir is special... */
193 			err =  ovl_mkdir_real(ofs, dir, &newdentry, attr->mode);
194 			break;
195 
196 		case S_IFCHR:
197 		case S_IFBLK:
198 		case S_IFIFO:
199 		case S_IFSOCK:
200 			err = ovl_do_mknod(ofs, dir, newdentry, attr->mode,
201 					   attr->rdev);
202 			break;
203 
204 		case S_IFLNK:
205 			err = ovl_do_symlink(ofs, dir, newdentry, attr->link);
206 			break;
207 
208 		default:
209 			err = -EPERM;
210 		}
211 	}
212 	if (!err && WARN_ON(!newdentry->d_inode)) {
213 		/*
214 		 * Not quite sure if non-instantiated dentry is legal or not.
215 		 * VFS doesn't seem to care so check and warn here.
216 		 */
217 		err = -EIO;
218 	}
219 out:
220 	if (err) {
221 		dput(newdentry);
222 		return ERR_PTR(err);
223 	}
224 	return newdentry;
225 }
226 
227 struct dentry *ovl_create_temp(struct ovl_fs *ofs, struct dentry *workdir,
228 			       struct ovl_cattr *attr)
229 {
230 	return ovl_create_real(ofs, d_inode(workdir),
231 			       ovl_lookup_temp(ofs, workdir), attr);
232 }
233 
234 static int ovl_set_opaque_xerr(struct dentry *dentry, struct dentry *upper,
235 			       int xerr)
236 {
237 	struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
238 	int err;
239 
240 	err = ovl_check_setxattr(ofs, upper, OVL_XATTR_OPAQUE, "y", 1, xerr);
241 	if (!err)
242 		ovl_dentry_set_opaque(dentry);
243 
244 	return err;
245 }
246 
247 static int ovl_set_opaque(struct dentry *dentry, struct dentry *upperdentry)
248 {
249 	/*
250 	 * Fail with -EIO when trying to create opaque dir and upper doesn't
251 	 * support xattrs. ovl_rename() calls ovl_set_opaque_xerr(-EXDEV) to
252 	 * return a specific error for noxattr case.
253 	 */
254 	return ovl_set_opaque_xerr(dentry, upperdentry, -EIO);
255 }
256 
257 /*
258  * Common operations required to be done after creation of file on upper.
259  * If @hardlink is false, then @inode is a pre-allocated inode, we may or
260  * may not use to instantiate the new dentry.
261  */
262 static int ovl_instantiate(struct dentry *dentry, struct inode *inode,
263 			   struct dentry *newdentry, bool hardlink)
264 {
265 	struct ovl_inode_params oip = {
266 		.upperdentry = newdentry,
267 		.newinode = inode,
268 	};
269 
270 	ovl_dir_modified(dentry->d_parent, false);
271 	ovl_dentry_set_upper_alias(dentry);
272 	ovl_dentry_update_reval(dentry, newdentry,
273 			DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE);
274 
275 	if (!hardlink) {
276 		/*
277 		 * ovl_obtain_alias() can be called after ovl_create_real()
278 		 * and before we get here, so we may get an inode from cache
279 		 * with the same real upperdentry that is not the inode we
280 		 * pre-allocated.  In this case we will use the cached inode
281 		 * to instantiate the new dentry.
282 		 *
283 		 * XXX: if we ever use ovl_obtain_alias() to decode directory
284 		 * file handles, need to use ovl_get_inode_locked() and
285 		 * d_instantiate_new() here to prevent from creating two
286 		 * hashed directory inode aliases.
287 		 */
288 		inode = ovl_get_inode(dentry->d_sb, &oip);
289 		if (IS_ERR(inode))
290 			return PTR_ERR(inode);
291 		if (inode == oip.newinode)
292 			ovl_set_flag(OVL_UPPERDATA, inode);
293 	} else {
294 		WARN_ON(ovl_inode_real(inode) != d_inode(newdentry));
295 		dput(newdentry);
296 		inc_nlink(inode);
297 	}
298 
299 	d_instantiate(dentry, inode);
300 	if (inode != oip.newinode) {
301 		pr_warn_ratelimited("newly created inode found in cache (%pd2)\n",
302 				    dentry);
303 	}
304 
305 	/* Force lookup of new upper hardlink to find its lower */
306 	if (hardlink)
307 		d_drop(dentry);
308 
309 	return 0;
310 }
311 
312 static bool ovl_type_merge(struct dentry *dentry)
313 {
314 	return OVL_TYPE_MERGE(ovl_path_type(dentry));
315 }
316 
317 static bool ovl_type_origin(struct dentry *dentry)
318 {
319 	return OVL_TYPE_ORIGIN(ovl_path_type(dentry));
320 }
321 
322 static int ovl_create_upper(struct dentry *dentry, struct inode *inode,
323 			    struct ovl_cattr *attr)
324 {
325 	struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
326 	struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
327 	struct inode *udir = upperdir->d_inode;
328 	struct dentry *newdentry;
329 	int err;
330 
331 	if (!attr->hardlink && !IS_POSIXACL(udir))
332 		attr->mode &= ~current_umask();
333 
334 	inode_lock_nested(udir, I_MUTEX_PARENT);
335 	newdentry = ovl_create_real(ofs, udir,
336 				    ovl_lookup_upper(ofs, dentry->d_name.name,
337 						     upperdir, dentry->d_name.len),
338 				    attr);
339 	err = PTR_ERR(newdentry);
340 	if (IS_ERR(newdentry))
341 		goto out_unlock;
342 
343 	if (ovl_type_merge(dentry->d_parent) && d_is_dir(newdentry) &&
344 	    !ovl_allow_offline_changes(ofs)) {
345 		/* Setting opaque here is just an optimization, allow to fail */
346 		ovl_set_opaque(dentry, newdentry);
347 	}
348 
349 	err = ovl_instantiate(dentry, inode, newdentry, !!attr->hardlink);
350 	if (err)
351 		goto out_cleanup;
352 out_unlock:
353 	inode_unlock(udir);
354 	return err;
355 
356 out_cleanup:
357 	ovl_cleanup(ofs, udir, newdentry);
358 	dput(newdentry);
359 	goto out_unlock;
360 }
361 
362 static struct dentry *ovl_clear_empty(struct dentry *dentry,
363 				      struct list_head *list)
364 {
365 	struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
366 	struct dentry *workdir = ovl_workdir(dentry);
367 	struct inode *wdir = workdir->d_inode;
368 	struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
369 	struct inode *udir = upperdir->d_inode;
370 	struct path upperpath;
371 	struct dentry *upper;
372 	struct dentry *opaquedir;
373 	struct kstat stat;
374 	int err;
375 
376 	if (WARN_ON(!workdir))
377 		return ERR_PTR(-EROFS);
378 
379 	err = ovl_lock_rename_workdir(workdir, upperdir);
380 	if (err)
381 		goto out;
382 
383 	ovl_path_upper(dentry, &upperpath);
384 	err = vfs_getattr(&upperpath, &stat,
385 			  STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
386 	if (err)
387 		goto out_unlock;
388 
389 	err = -ESTALE;
390 	if (!S_ISDIR(stat.mode))
391 		goto out_unlock;
392 	upper = upperpath.dentry;
393 	if (upper->d_parent->d_inode != udir)
394 		goto out_unlock;
395 
396 	opaquedir = ovl_create_temp(ofs, workdir, OVL_CATTR(stat.mode));
397 	err = PTR_ERR(opaquedir);
398 	if (IS_ERR(opaquedir))
399 		goto out_unlock;
400 
401 	err = ovl_copy_xattr(dentry->d_sb, &upperpath, opaquedir);
402 	if (err)
403 		goto out_cleanup;
404 
405 	err = ovl_set_opaque(dentry, opaquedir);
406 	if (err)
407 		goto out_cleanup;
408 
409 	inode_lock(opaquedir->d_inode);
410 	err = ovl_set_attr(ofs, opaquedir, &stat);
411 	inode_unlock(opaquedir->d_inode);
412 	if (err)
413 		goto out_cleanup;
414 
415 	err = ovl_do_rename(ofs, wdir, opaquedir, udir, upper, RENAME_EXCHANGE);
416 	if (err)
417 		goto out_cleanup;
418 
419 	ovl_cleanup_whiteouts(ofs, upper, list);
420 	ovl_cleanup(ofs, wdir, upper);
421 	unlock_rename(workdir, upperdir);
422 
423 	/* dentry's upper doesn't match now, get rid of it */
424 	d_drop(dentry);
425 
426 	return opaquedir;
427 
428 out_cleanup:
429 	ovl_cleanup(ofs, wdir, opaquedir);
430 	dput(opaquedir);
431 out_unlock:
432 	unlock_rename(workdir, upperdir);
433 out:
434 	return ERR_PTR(err);
435 }
436 
437 static int ovl_set_upper_acl(struct ovl_fs *ofs, struct dentry *upperdentry,
438 			     const char *acl_name, struct posix_acl *acl)
439 {
440 	if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !acl)
441 		return 0;
442 
443 	return ovl_do_set_acl(ofs, upperdentry, acl_name, acl);
444 }
445 
446 static int ovl_create_over_whiteout(struct dentry *dentry, struct inode *inode,
447 				    struct ovl_cattr *cattr)
448 {
449 	struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
450 	struct dentry *workdir = ovl_workdir(dentry);
451 	struct inode *wdir = workdir->d_inode;
452 	struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
453 	struct inode *udir = upperdir->d_inode;
454 	struct dentry *upper;
455 	struct dentry *newdentry;
456 	int err;
457 	struct posix_acl *acl, *default_acl;
458 	bool hardlink = !!cattr->hardlink;
459 
460 	if (WARN_ON(!workdir))
461 		return -EROFS;
462 
463 	if (!hardlink) {
464 		err = posix_acl_create(dentry->d_parent->d_inode,
465 				       &cattr->mode, &default_acl, &acl);
466 		if (err)
467 			return err;
468 	}
469 
470 	err = ovl_lock_rename_workdir(workdir, upperdir);
471 	if (err)
472 		goto out;
473 
474 	upper = ovl_lookup_upper(ofs, dentry->d_name.name, upperdir,
475 				 dentry->d_name.len);
476 	err = PTR_ERR(upper);
477 	if (IS_ERR(upper))
478 		goto out_unlock;
479 
480 	err = -ESTALE;
481 	if (d_is_negative(upper) || !IS_WHITEOUT(d_inode(upper)))
482 		goto out_dput;
483 
484 	newdentry = ovl_create_temp(ofs, workdir, cattr);
485 	err = PTR_ERR(newdentry);
486 	if (IS_ERR(newdentry))
487 		goto out_dput;
488 
489 	/*
490 	 * mode could have been mutilated due to umask (e.g. sgid directory)
491 	 */
492 	if (!hardlink &&
493 	    !S_ISLNK(cattr->mode) &&
494 	    newdentry->d_inode->i_mode != cattr->mode) {
495 		struct iattr attr = {
496 			.ia_valid = ATTR_MODE,
497 			.ia_mode = cattr->mode,
498 		};
499 		inode_lock(newdentry->d_inode);
500 		err = ovl_do_notify_change(ofs, newdentry, &attr);
501 		inode_unlock(newdentry->d_inode);
502 		if (err)
503 			goto out_cleanup;
504 	}
505 	if (!hardlink) {
506 		err = ovl_set_upper_acl(ofs, newdentry,
507 					XATTR_NAME_POSIX_ACL_ACCESS, acl);
508 		if (err)
509 			goto out_cleanup;
510 
511 		err = ovl_set_upper_acl(ofs, newdentry,
512 					XATTR_NAME_POSIX_ACL_DEFAULT, default_acl);
513 		if (err)
514 			goto out_cleanup;
515 	}
516 
517 	if (!hardlink && S_ISDIR(cattr->mode)) {
518 		err = ovl_set_opaque(dentry, newdentry);
519 		if (err)
520 			goto out_cleanup;
521 
522 		err = ovl_do_rename(ofs, wdir, newdentry, udir, upper,
523 				    RENAME_EXCHANGE);
524 		if (err)
525 			goto out_cleanup;
526 
527 		ovl_cleanup(ofs, wdir, upper);
528 	} else {
529 		err = ovl_do_rename(ofs, wdir, newdentry, udir, upper, 0);
530 		if (err)
531 			goto out_cleanup;
532 	}
533 	err = ovl_instantiate(dentry, inode, newdentry, hardlink);
534 	if (err) {
535 		ovl_cleanup(ofs, udir, newdentry);
536 		dput(newdentry);
537 	}
538 out_dput:
539 	dput(upper);
540 out_unlock:
541 	unlock_rename(workdir, upperdir);
542 out:
543 	if (!hardlink) {
544 		posix_acl_release(acl);
545 		posix_acl_release(default_acl);
546 	}
547 	return err;
548 
549 out_cleanup:
550 	ovl_cleanup(ofs, wdir, newdentry);
551 	dput(newdentry);
552 	goto out_dput;
553 }
554 
555 static int ovl_create_or_link(struct dentry *dentry, struct inode *inode,
556 			      struct ovl_cattr *attr, bool origin)
557 {
558 	int err;
559 	const struct cred *old_cred;
560 	struct cred *override_cred;
561 	struct dentry *parent = dentry->d_parent;
562 
563 	err = ovl_copy_up(parent);
564 	if (err)
565 		return err;
566 
567 	old_cred = ovl_override_creds(dentry->d_sb);
568 
569 	/*
570 	 * When linking a file with copy up origin into a new parent, mark the
571 	 * new parent dir "impure".
572 	 */
573 	if (origin) {
574 		err = ovl_set_impure(parent, ovl_dentry_upper(parent));
575 		if (err)
576 			goto out_revert_creds;
577 	}
578 
579 	if (!attr->hardlink) {
580 		err = -ENOMEM;
581 		override_cred = prepare_creds();
582 		if (!override_cred)
583 			goto out_revert_creds;
584 		/*
585 		 * In the creation cases(create, mkdir, mknod, symlink),
586 		 * ovl should transfer current's fs{u,g}id to underlying
587 		 * fs. Because underlying fs want to initialize its new
588 		 * inode owner using current's fs{u,g}id. And in this
589 		 * case, the @inode is a new inode that is initialized
590 		 * in inode_init_owner() to current's fs{u,g}id. So use
591 		 * the inode's i_{u,g}id to override the cred's fs{u,g}id.
592 		 *
593 		 * But in the other hardlink case, ovl_link() does not
594 		 * create a new inode, so just use the ovl mounter's
595 		 * fs{u,g}id.
596 		 */
597 		override_cred->fsuid = inode->i_uid;
598 		override_cred->fsgid = inode->i_gid;
599 		err = security_dentry_create_files_as(dentry,
600 				attr->mode, &dentry->d_name, old_cred,
601 				override_cred);
602 		if (err) {
603 			put_cred(override_cred);
604 			goto out_revert_creds;
605 		}
606 		put_cred(override_creds(override_cred));
607 		put_cred(override_cred);
608 	}
609 
610 	if (!ovl_dentry_is_whiteout(dentry))
611 		err = ovl_create_upper(dentry, inode, attr);
612 	else
613 		err = ovl_create_over_whiteout(dentry, inode, attr);
614 
615 out_revert_creds:
616 	revert_creds(old_cred);
617 	return err;
618 }
619 
620 static int ovl_create_object(struct dentry *dentry, int mode, dev_t rdev,
621 			     const char *link)
622 {
623 	int err;
624 	struct inode *inode;
625 	struct ovl_cattr attr = {
626 		.rdev = rdev,
627 		.link = link,
628 	};
629 
630 	err = ovl_want_write(dentry);
631 	if (err)
632 		goto out;
633 
634 	/* Preallocate inode to be used by ovl_get_inode() */
635 	err = -ENOMEM;
636 	inode = ovl_new_inode(dentry->d_sb, mode, rdev);
637 	if (!inode)
638 		goto out_drop_write;
639 
640 	spin_lock(&inode->i_lock);
641 	inode->i_state |= I_CREATING;
642 	spin_unlock(&inode->i_lock);
643 
644 	inode_init_owner(&init_user_ns, inode, dentry->d_parent->d_inode, mode);
645 	attr.mode = inode->i_mode;
646 
647 	err = ovl_create_or_link(dentry, inode, &attr, false);
648 	/* Did we end up using the preallocated inode? */
649 	if (inode != d_inode(dentry))
650 		iput(inode);
651 
652 out_drop_write:
653 	ovl_drop_write(dentry);
654 out:
655 	return err;
656 }
657 
658 static int ovl_create(struct user_namespace *mnt_userns, struct inode *dir,
659 		      struct dentry *dentry, umode_t mode, bool excl)
660 {
661 	return ovl_create_object(dentry, (mode & 07777) | S_IFREG, 0, NULL);
662 }
663 
664 static int ovl_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
665 		     struct dentry *dentry, umode_t mode)
666 {
667 	return ovl_create_object(dentry, (mode & 07777) | S_IFDIR, 0, NULL);
668 }
669 
670 static int ovl_mknod(struct user_namespace *mnt_userns, struct inode *dir,
671 		     struct dentry *dentry, umode_t mode, dev_t rdev)
672 {
673 	/* Don't allow creation of "whiteout" on overlay */
674 	if (S_ISCHR(mode) && rdev == WHITEOUT_DEV)
675 		return -EPERM;
676 
677 	return ovl_create_object(dentry, mode, rdev, NULL);
678 }
679 
680 static int ovl_symlink(struct user_namespace *mnt_userns, struct inode *dir,
681 		       struct dentry *dentry, const char *link)
682 {
683 	return ovl_create_object(dentry, S_IFLNK, 0, link);
684 }
685 
686 static int ovl_set_link_redirect(struct dentry *dentry)
687 {
688 	const struct cred *old_cred;
689 	int err;
690 
691 	old_cred = ovl_override_creds(dentry->d_sb);
692 	err = ovl_set_redirect(dentry, false);
693 	revert_creds(old_cred);
694 
695 	return err;
696 }
697 
698 static int ovl_link(struct dentry *old, struct inode *newdir,
699 		    struct dentry *new)
700 {
701 	int err;
702 	struct inode *inode;
703 
704 	err = ovl_want_write(old);
705 	if (err)
706 		goto out;
707 
708 	err = ovl_copy_up(old);
709 	if (err)
710 		goto out_drop_write;
711 
712 	err = ovl_copy_up(new->d_parent);
713 	if (err)
714 		goto out_drop_write;
715 
716 	if (ovl_is_metacopy_dentry(old)) {
717 		err = ovl_set_link_redirect(old);
718 		if (err)
719 			goto out_drop_write;
720 	}
721 
722 	err = ovl_nlink_start(old);
723 	if (err)
724 		goto out_drop_write;
725 
726 	inode = d_inode(old);
727 	ihold(inode);
728 
729 	err = ovl_create_or_link(new, inode,
730 			&(struct ovl_cattr) {.hardlink = ovl_dentry_upper(old)},
731 			ovl_type_origin(old));
732 	if (err)
733 		iput(inode);
734 
735 	ovl_nlink_end(old);
736 out_drop_write:
737 	ovl_drop_write(old);
738 out:
739 	return err;
740 }
741 
742 static bool ovl_matches_upper(struct dentry *dentry, struct dentry *upper)
743 {
744 	return d_inode(ovl_dentry_upper(dentry)) == d_inode(upper);
745 }
746 
747 static int ovl_remove_and_whiteout(struct dentry *dentry,
748 				   struct list_head *list)
749 {
750 	struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
751 	struct dentry *workdir = ovl_workdir(dentry);
752 	struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
753 	struct dentry *upper;
754 	struct dentry *opaquedir = NULL;
755 	int err;
756 
757 	if (WARN_ON(!workdir))
758 		return -EROFS;
759 
760 	if (!list_empty(list)) {
761 		opaquedir = ovl_clear_empty(dentry, list);
762 		err = PTR_ERR(opaquedir);
763 		if (IS_ERR(opaquedir))
764 			goto out;
765 	}
766 
767 	err = ovl_lock_rename_workdir(workdir, upperdir);
768 	if (err)
769 		goto out_dput;
770 
771 	upper = ovl_lookup_upper(ofs, dentry->d_name.name, upperdir,
772 				 dentry->d_name.len);
773 	err = PTR_ERR(upper);
774 	if (IS_ERR(upper))
775 		goto out_unlock;
776 
777 	err = -ESTALE;
778 	if ((opaquedir && upper != opaquedir) ||
779 	    (!opaquedir && ovl_dentry_upper(dentry) &&
780 	     !ovl_matches_upper(dentry, upper))) {
781 		goto out_dput_upper;
782 	}
783 
784 	err = ovl_cleanup_and_whiteout(ofs, d_inode(upperdir), upper);
785 	if (err)
786 		goto out_d_drop;
787 
788 	ovl_dir_modified(dentry->d_parent, true);
789 out_d_drop:
790 	d_drop(dentry);
791 out_dput_upper:
792 	dput(upper);
793 out_unlock:
794 	unlock_rename(workdir, upperdir);
795 out_dput:
796 	dput(opaquedir);
797 out:
798 	return err;
799 }
800 
801 static int ovl_remove_upper(struct dentry *dentry, bool is_dir,
802 			    struct list_head *list)
803 {
804 	struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
805 	struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
806 	struct inode *dir = upperdir->d_inode;
807 	struct dentry *upper;
808 	struct dentry *opaquedir = NULL;
809 	int err;
810 
811 	if (!list_empty(list)) {
812 		opaquedir = ovl_clear_empty(dentry, list);
813 		err = PTR_ERR(opaquedir);
814 		if (IS_ERR(opaquedir))
815 			goto out;
816 	}
817 
818 	inode_lock_nested(dir, I_MUTEX_PARENT);
819 	upper = ovl_lookup_upper(ofs, dentry->d_name.name, upperdir,
820 				 dentry->d_name.len);
821 	err = PTR_ERR(upper);
822 	if (IS_ERR(upper))
823 		goto out_unlock;
824 
825 	err = -ESTALE;
826 	if ((opaquedir && upper != opaquedir) ||
827 	    (!opaquedir && !ovl_matches_upper(dentry, upper)))
828 		goto out_dput_upper;
829 
830 	if (is_dir)
831 		err = ovl_do_rmdir(ofs, dir, upper);
832 	else
833 		err = ovl_do_unlink(ofs, dir, upper);
834 	ovl_dir_modified(dentry->d_parent, ovl_type_origin(dentry));
835 
836 	/*
837 	 * Keeping this dentry hashed would mean having to release
838 	 * upperpath/lowerpath, which could only be done if we are the
839 	 * sole user of this dentry.  Too tricky...  Just unhash for
840 	 * now.
841 	 */
842 	if (!err)
843 		d_drop(dentry);
844 out_dput_upper:
845 	dput(upper);
846 out_unlock:
847 	inode_unlock(dir);
848 	dput(opaquedir);
849 out:
850 	return err;
851 }
852 
853 static bool ovl_pure_upper(struct dentry *dentry)
854 {
855 	return !ovl_dentry_lower(dentry) &&
856 	       !ovl_test_flag(OVL_WHITEOUTS, d_inode(dentry));
857 }
858 
859 static void ovl_drop_nlink(struct dentry *dentry)
860 {
861 	struct inode *inode = d_inode(dentry);
862 	struct dentry *alias;
863 
864 	/* Try to find another, hashed alias */
865 	spin_lock(&inode->i_lock);
866 	hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
867 		if (alias != dentry && !d_unhashed(alias))
868 			break;
869 	}
870 	spin_unlock(&inode->i_lock);
871 
872 	/*
873 	 * Changes to underlying layers may cause i_nlink to lose sync with
874 	 * reality.  In this case prevent the link count from going to zero
875 	 * prematurely.
876 	 */
877 	if (inode->i_nlink > !!alias)
878 		drop_nlink(inode);
879 }
880 
881 static int ovl_do_remove(struct dentry *dentry, bool is_dir)
882 {
883 	int err;
884 	const struct cred *old_cred;
885 	bool lower_positive = ovl_lower_positive(dentry);
886 	LIST_HEAD(list);
887 
888 	/* No need to clean pure upper removed by vfs_rmdir() */
889 	if (is_dir && (lower_positive || !ovl_pure_upper(dentry))) {
890 		err = ovl_check_empty_dir(dentry, &list);
891 		if (err)
892 			goto out;
893 	}
894 
895 	err = ovl_want_write(dentry);
896 	if (err)
897 		goto out;
898 
899 	err = ovl_copy_up(dentry->d_parent);
900 	if (err)
901 		goto out_drop_write;
902 
903 	err = ovl_nlink_start(dentry);
904 	if (err)
905 		goto out_drop_write;
906 
907 	old_cred = ovl_override_creds(dentry->d_sb);
908 	if (!lower_positive)
909 		err = ovl_remove_upper(dentry, is_dir, &list);
910 	else
911 		err = ovl_remove_and_whiteout(dentry, &list);
912 	revert_creds(old_cred);
913 	if (!err) {
914 		if (is_dir)
915 			clear_nlink(dentry->d_inode);
916 		else
917 			ovl_drop_nlink(dentry);
918 	}
919 	ovl_nlink_end(dentry);
920 
921 	/*
922 	 * Copy ctime
923 	 *
924 	 * Note: we fail to update ctime if there was no copy-up, only a
925 	 * whiteout
926 	 */
927 	if (ovl_dentry_upper(dentry))
928 		ovl_copyattr(d_inode(dentry));
929 
930 out_drop_write:
931 	ovl_drop_write(dentry);
932 out:
933 	ovl_cache_free(&list);
934 	return err;
935 }
936 
937 static int ovl_unlink(struct inode *dir, struct dentry *dentry)
938 {
939 	return ovl_do_remove(dentry, false);
940 }
941 
942 static int ovl_rmdir(struct inode *dir, struct dentry *dentry)
943 {
944 	return ovl_do_remove(dentry, true);
945 }
946 
947 static bool ovl_type_merge_or_lower(struct dentry *dentry)
948 {
949 	enum ovl_path_type type = ovl_path_type(dentry);
950 
951 	return OVL_TYPE_MERGE(type) || !OVL_TYPE_UPPER(type);
952 }
953 
954 static bool ovl_can_move(struct dentry *dentry)
955 {
956 	return ovl_redirect_dir(dentry->d_sb) ||
957 		!d_is_dir(dentry) || !ovl_type_merge_or_lower(dentry);
958 }
959 
960 static char *ovl_get_redirect(struct dentry *dentry, bool abs_redirect)
961 {
962 	char *buf, *ret;
963 	struct dentry *d, *tmp;
964 	int buflen = ovl_redirect_max + 1;
965 
966 	if (!abs_redirect) {
967 		ret = kstrndup(dentry->d_name.name, dentry->d_name.len,
968 			       GFP_KERNEL);
969 		goto out;
970 	}
971 
972 	buf = ret = kmalloc(buflen, GFP_KERNEL);
973 	if (!buf)
974 		goto out;
975 
976 	buflen--;
977 	buf[buflen] = '\0';
978 	for (d = dget(dentry); !IS_ROOT(d);) {
979 		const char *name;
980 		int thislen;
981 
982 		spin_lock(&d->d_lock);
983 		name = ovl_dentry_get_redirect(d);
984 		if (name) {
985 			thislen = strlen(name);
986 		} else {
987 			name = d->d_name.name;
988 			thislen = d->d_name.len;
989 		}
990 
991 		/* If path is too long, fall back to userspace move */
992 		if (thislen + (name[0] != '/') > buflen) {
993 			ret = ERR_PTR(-EXDEV);
994 			spin_unlock(&d->d_lock);
995 			goto out_put;
996 		}
997 
998 		buflen -= thislen;
999 		memcpy(&buf[buflen], name, thislen);
1000 		spin_unlock(&d->d_lock);
1001 		tmp = dget_parent(d);
1002 
1003 		dput(d);
1004 		d = tmp;
1005 
1006 		/* Absolute redirect: finished */
1007 		if (buf[buflen] == '/')
1008 			break;
1009 		buflen--;
1010 		buf[buflen] = '/';
1011 	}
1012 	ret = kstrdup(&buf[buflen], GFP_KERNEL);
1013 out_put:
1014 	dput(d);
1015 	kfree(buf);
1016 out:
1017 	return ret ? ret : ERR_PTR(-ENOMEM);
1018 }
1019 
1020 static bool ovl_need_absolute_redirect(struct dentry *dentry, bool samedir)
1021 {
1022 	struct dentry *lowerdentry;
1023 
1024 	if (!samedir)
1025 		return true;
1026 
1027 	if (d_is_dir(dentry))
1028 		return false;
1029 
1030 	/*
1031 	 * For non-dir hardlinked files, we need absolute redirects
1032 	 * in general as two upper hardlinks could be in different
1033 	 * dirs. We could put a relative redirect now and convert
1034 	 * it to absolute redirect later. But when nlink > 1 and
1035 	 * indexing is on, that means relative redirect needs to be
1036 	 * converted to absolute during copy up of another lower
1037 	 * hardllink as well.
1038 	 *
1039 	 * So without optimizing too much, just check if lower is
1040 	 * a hard link or not. If lower is hard link, put absolute
1041 	 * redirect.
1042 	 */
1043 	lowerdentry = ovl_dentry_lower(dentry);
1044 	return (d_inode(lowerdentry)->i_nlink > 1);
1045 }
1046 
1047 static int ovl_set_redirect(struct dentry *dentry, bool samedir)
1048 {
1049 	int err;
1050 	struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
1051 	const char *redirect = ovl_dentry_get_redirect(dentry);
1052 	bool absolute_redirect = ovl_need_absolute_redirect(dentry, samedir);
1053 
1054 	if (redirect && (!absolute_redirect || redirect[0] == '/'))
1055 		return 0;
1056 
1057 	redirect = ovl_get_redirect(dentry, absolute_redirect);
1058 	if (IS_ERR(redirect))
1059 		return PTR_ERR(redirect);
1060 
1061 	err = ovl_check_setxattr(ofs, ovl_dentry_upper(dentry),
1062 				 OVL_XATTR_REDIRECT,
1063 				 redirect, strlen(redirect), -EXDEV);
1064 	if (!err) {
1065 		spin_lock(&dentry->d_lock);
1066 		ovl_dentry_set_redirect(dentry, redirect);
1067 		spin_unlock(&dentry->d_lock);
1068 	} else {
1069 		kfree(redirect);
1070 		pr_warn_ratelimited("failed to set redirect (%i)\n",
1071 				    err);
1072 		/* Fall back to userspace copy-up */
1073 		err = -EXDEV;
1074 	}
1075 	return err;
1076 }
1077 
1078 static int ovl_rename(struct user_namespace *mnt_userns, struct inode *olddir,
1079 		      struct dentry *old, struct inode *newdir,
1080 		      struct dentry *new, unsigned int flags)
1081 {
1082 	int err;
1083 	struct dentry *old_upperdir;
1084 	struct dentry *new_upperdir;
1085 	struct dentry *olddentry;
1086 	struct dentry *newdentry;
1087 	struct dentry *trap;
1088 	bool old_opaque;
1089 	bool new_opaque;
1090 	bool cleanup_whiteout = false;
1091 	bool update_nlink = false;
1092 	bool overwrite = !(flags & RENAME_EXCHANGE);
1093 	bool is_dir = d_is_dir(old);
1094 	bool new_is_dir = d_is_dir(new);
1095 	bool samedir = olddir == newdir;
1096 	struct dentry *opaquedir = NULL;
1097 	const struct cred *old_cred = NULL;
1098 	struct ovl_fs *ofs = OVL_FS(old->d_sb);
1099 	LIST_HEAD(list);
1100 
1101 	err = -EINVAL;
1102 	if (flags & ~(RENAME_EXCHANGE | RENAME_NOREPLACE))
1103 		goto out;
1104 
1105 	flags &= ~RENAME_NOREPLACE;
1106 
1107 	/* Don't copy up directory trees */
1108 	err = -EXDEV;
1109 	if (!ovl_can_move(old))
1110 		goto out;
1111 	if (!overwrite && !ovl_can_move(new))
1112 		goto out;
1113 
1114 	if (overwrite && new_is_dir && !ovl_pure_upper(new)) {
1115 		err = ovl_check_empty_dir(new, &list);
1116 		if (err)
1117 			goto out;
1118 	}
1119 
1120 	if (overwrite) {
1121 		if (ovl_lower_positive(old)) {
1122 			if (!ovl_dentry_is_whiteout(new)) {
1123 				/* Whiteout source */
1124 				flags |= RENAME_WHITEOUT;
1125 			} else {
1126 				/* Switch whiteouts */
1127 				flags |= RENAME_EXCHANGE;
1128 			}
1129 		} else if (is_dir && ovl_dentry_is_whiteout(new)) {
1130 			flags |= RENAME_EXCHANGE;
1131 			cleanup_whiteout = true;
1132 		}
1133 	}
1134 
1135 	err = ovl_want_write(old);
1136 	if (err)
1137 		goto out;
1138 
1139 	err = ovl_copy_up(old);
1140 	if (err)
1141 		goto out_drop_write;
1142 
1143 	err = ovl_copy_up(new->d_parent);
1144 	if (err)
1145 		goto out_drop_write;
1146 	if (!overwrite) {
1147 		err = ovl_copy_up(new);
1148 		if (err)
1149 			goto out_drop_write;
1150 	} else if (d_inode(new)) {
1151 		err = ovl_nlink_start(new);
1152 		if (err)
1153 			goto out_drop_write;
1154 
1155 		update_nlink = true;
1156 	}
1157 
1158 	old_cred = ovl_override_creds(old->d_sb);
1159 
1160 	if (!list_empty(&list)) {
1161 		opaquedir = ovl_clear_empty(new, &list);
1162 		err = PTR_ERR(opaquedir);
1163 		if (IS_ERR(opaquedir)) {
1164 			opaquedir = NULL;
1165 			goto out_revert_creds;
1166 		}
1167 	}
1168 
1169 	old_upperdir = ovl_dentry_upper(old->d_parent);
1170 	new_upperdir = ovl_dentry_upper(new->d_parent);
1171 
1172 	if (!samedir) {
1173 		/*
1174 		 * When moving a merge dir or non-dir with copy up origin into
1175 		 * a new parent, we are marking the new parent dir "impure".
1176 		 * When ovl_iterate() iterates an "impure" upper dir, it will
1177 		 * lookup the origin inodes of the entries to fill d_ino.
1178 		 */
1179 		if (ovl_type_origin(old)) {
1180 			err = ovl_set_impure(new->d_parent, new_upperdir);
1181 			if (err)
1182 				goto out_revert_creds;
1183 		}
1184 		if (!overwrite && ovl_type_origin(new)) {
1185 			err = ovl_set_impure(old->d_parent, old_upperdir);
1186 			if (err)
1187 				goto out_revert_creds;
1188 		}
1189 	}
1190 
1191 	trap = lock_rename(new_upperdir, old_upperdir);
1192 
1193 	olddentry = ovl_lookup_upper(ofs, old->d_name.name, old_upperdir,
1194 				     old->d_name.len);
1195 	err = PTR_ERR(olddentry);
1196 	if (IS_ERR(olddentry))
1197 		goto out_unlock;
1198 
1199 	err = -ESTALE;
1200 	if (!ovl_matches_upper(old, olddentry))
1201 		goto out_dput_old;
1202 
1203 	newdentry = ovl_lookup_upper(ofs, new->d_name.name, new_upperdir,
1204 				     new->d_name.len);
1205 	err = PTR_ERR(newdentry);
1206 	if (IS_ERR(newdentry))
1207 		goto out_dput_old;
1208 
1209 	old_opaque = ovl_dentry_is_opaque(old);
1210 	new_opaque = ovl_dentry_is_opaque(new);
1211 
1212 	err = -ESTALE;
1213 	if (d_inode(new) && ovl_dentry_upper(new)) {
1214 		if (opaquedir) {
1215 			if (newdentry != opaquedir)
1216 				goto out_dput;
1217 		} else {
1218 			if (!ovl_matches_upper(new, newdentry))
1219 				goto out_dput;
1220 		}
1221 	} else {
1222 		if (!d_is_negative(newdentry)) {
1223 			if (!new_opaque || !ovl_is_whiteout(newdentry))
1224 				goto out_dput;
1225 		} else {
1226 			if (flags & RENAME_EXCHANGE)
1227 				goto out_dput;
1228 		}
1229 	}
1230 
1231 	if (olddentry == trap)
1232 		goto out_dput;
1233 	if (newdentry == trap)
1234 		goto out_dput;
1235 
1236 	if (olddentry->d_inode == newdentry->d_inode)
1237 		goto out_dput;
1238 
1239 	err = 0;
1240 	if (ovl_type_merge_or_lower(old))
1241 		err = ovl_set_redirect(old, samedir);
1242 	else if (is_dir && !old_opaque && ovl_type_merge(new->d_parent))
1243 		err = ovl_set_opaque_xerr(old, olddentry, -EXDEV);
1244 	if (err)
1245 		goto out_dput;
1246 
1247 	if (!overwrite && ovl_type_merge_or_lower(new))
1248 		err = ovl_set_redirect(new, samedir);
1249 	else if (!overwrite && new_is_dir && !new_opaque &&
1250 		 ovl_type_merge(old->d_parent))
1251 		err = ovl_set_opaque_xerr(new, newdentry, -EXDEV);
1252 	if (err)
1253 		goto out_dput;
1254 
1255 	err = ovl_do_rename(ofs, old_upperdir->d_inode, olddentry,
1256 			    new_upperdir->d_inode, newdentry, flags);
1257 	if (err)
1258 		goto out_dput;
1259 
1260 	if (cleanup_whiteout)
1261 		ovl_cleanup(ofs, old_upperdir->d_inode, newdentry);
1262 
1263 	if (overwrite && d_inode(new)) {
1264 		if (new_is_dir)
1265 			clear_nlink(d_inode(new));
1266 		else
1267 			ovl_drop_nlink(new);
1268 	}
1269 
1270 	ovl_dir_modified(old->d_parent, ovl_type_origin(old) ||
1271 			 (!overwrite && ovl_type_origin(new)));
1272 	ovl_dir_modified(new->d_parent, ovl_type_origin(old) ||
1273 			 (d_inode(new) && ovl_type_origin(new)));
1274 
1275 	/* copy ctime: */
1276 	ovl_copyattr(d_inode(old));
1277 	if (d_inode(new) && ovl_dentry_upper(new))
1278 		ovl_copyattr(d_inode(new));
1279 
1280 out_dput:
1281 	dput(newdentry);
1282 out_dput_old:
1283 	dput(olddentry);
1284 out_unlock:
1285 	unlock_rename(new_upperdir, old_upperdir);
1286 out_revert_creds:
1287 	revert_creds(old_cred);
1288 	if (update_nlink)
1289 		ovl_nlink_end(new);
1290 out_drop_write:
1291 	ovl_drop_write(old);
1292 out:
1293 	dput(opaquedir);
1294 	ovl_cache_free(&list);
1295 	return err;
1296 }
1297 
1298 const struct inode_operations ovl_dir_inode_operations = {
1299 	.lookup		= ovl_lookup,
1300 	.mkdir		= ovl_mkdir,
1301 	.symlink	= ovl_symlink,
1302 	.unlink		= ovl_unlink,
1303 	.rmdir		= ovl_rmdir,
1304 	.rename		= ovl_rename,
1305 	.link		= ovl_link,
1306 	.setattr	= ovl_setattr,
1307 	.create		= ovl_create,
1308 	.mknod		= ovl_mknod,
1309 	.permission	= ovl_permission,
1310 	.getattr	= ovl_getattr,
1311 	.listxattr	= ovl_listxattr,
1312 	.get_inode_acl	= ovl_get_inode_acl,
1313 	.get_acl	= ovl_get_acl,
1314 	.set_acl	= ovl_set_acl,
1315 	.update_time	= ovl_update_time,
1316 	.fileattr_get	= ovl_fileattr_get,
1317 	.fileattr_set	= ovl_fileattr_set,
1318 };
1319