xref: /linux/fs/overlayfs/dir.c (revision dbcedec3a31119d7594baacc743300d127c99c56)
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->no_shared_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->no_shared_whiteout = true;
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_init_reval(dentry, newdentry, NULL);
273 
274 	if (!hardlink) {
275 		/*
276 		 * ovl_obtain_alias() can be called after ovl_create_real()
277 		 * and before we get here, so we may get an inode from cache
278 		 * with the same real upperdentry that is not the inode we
279 		 * pre-allocated.  In this case we will use the cached inode
280 		 * to instantiate the new dentry.
281 		 *
282 		 * XXX: if we ever use ovl_obtain_alias() to decode directory
283 		 * file handles, need to use ovl_get_inode_locked() and
284 		 * d_instantiate_new() here to prevent from creating two
285 		 * hashed directory inode aliases.
286 		 */
287 		inode = ovl_get_inode(dentry->d_sb, &oip);
288 		if (IS_ERR(inode))
289 			return PTR_ERR(inode);
290 		if (inode == oip.newinode)
291 			ovl_set_flag(OVL_UPPERDATA, inode);
292 	} else {
293 		WARN_ON(ovl_inode_real(inode) != d_inode(newdentry));
294 		dput(newdentry);
295 		inc_nlink(inode);
296 	}
297 
298 	d_instantiate(dentry, inode);
299 	if (inode != oip.newinode) {
300 		pr_warn_ratelimited("newly created inode found in cache (%pd2)\n",
301 				    dentry);
302 	}
303 
304 	/* Force lookup of new upper hardlink to find its lower */
305 	if (hardlink)
306 		d_drop(dentry);
307 
308 	return 0;
309 }
310 
311 static bool ovl_type_merge(struct dentry *dentry)
312 {
313 	return OVL_TYPE_MERGE(ovl_path_type(dentry));
314 }
315 
316 static bool ovl_type_origin(struct dentry *dentry)
317 {
318 	return OVL_TYPE_ORIGIN(ovl_path_type(dentry));
319 }
320 
321 static int ovl_create_upper(struct dentry *dentry, struct inode *inode,
322 			    struct ovl_cattr *attr)
323 {
324 	struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
325 	struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
326 	struct inode *udir = upperdir->d_inode;
327 	struct dentry *newdentry;
328 	int err;
329 
330 	if (!attr->hardlink && !IS_POSIXACL(udir))
331 		attr->mode &= ~current_umask();
332 
333 	inode_lock_nested(udir, I_MUTEX_PARENT);
334 	newdentry = ovl_create_real(ofs, udir,
335 				    ovl_lookup_upper(ofs, dentry->d_name.name,
336 						     upperdir, dentry->d_name.len),
337 				    attr);
338 	err = PTR_ERR(newdentry);
339 	if (IS_ERR(newdentry))
340 		goto out_unlock;
341 
342 	if (ovl_type_merge(dentry->d_parent) && d_is_dir(newdentry) &&
343 	    !ovl_allow_offline_changes(ofs)) {
344 		/* Setting opaque here is just an optimization, allow to fail */
345 		ovl_set_opaque(dentry, newdentry);
346 	}
347 
348 	err = ovl_instantiate(dentry, inode, newdentry, !!attr->hardlink);
349 	if (err)
350 		goto out_cleanup;
351 out_unlock:
352 	inode_unlock(udir);
353 	return err;
354 
355 out_cleanup:
356 	ovl_cleanup(ofs, udir, newdentry);
357 	dput(newdentry);
358 	goto out_unlock;
359 }
360 
361 static struct dentry *ovl_clear_empty(struct dentry *dentry,
362 				      struct list_head *list)
363 {
364 	struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
365 	struct dentry *workdir = ovl_workdir(dentry);
366 	struct inode *wdir = workdir->d_inode;
367 	struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
368 	struct inode *udir = upperdir->d_inode;
369 	struct path upperpath;
370 	struct dentry *upper;
371 	struct dentry *opaquedir;
372 	struct kstat stat;
373 	int err;
374 
375 	if (WARN_ON(!workdir))
376 		return ERR_PTR(-EROFS);
377 
378 	err = ovl_lock_rename_workdir(workdir, upperdir);
379 	if (err)
380 		goto out;
381 
382 	ovl_path_upper(dentry, &upperpath);
383 	err = vfs_getattr(&upperpath, &stat,
384 			  STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
385 	if (err)
386 		goto out_unlock;
387 
388 	err = -ESTALE;
389 	if (!S_ISDIR(stat.mode))
390 		goto out_unlock;
391 	upper = upperpath.dentry;
392 	if (upper->d_parent->d_inode != udir)
393 		goto out_unlock;
394 
395 	opaquedir = ovl_create_temp(ofs, workdir, OVL_CATTR(stat.mode));
396 	err = PTR_ERR(opaquedir);
397 	if (IS_ERR(opaquedir))
398 		goto out_unlock;
399 
400 	err = ovl_copy_xattr(dentry->d_sb, &upperpath, opaquedir);
401 	if (err)
402 		goto out_cleanup;
403 
404 	err = ovl_set_opaque(dentry, opaquedir);
405 	if (err)
406 		goto out_cleanup;
407 
408 	inode_lock(opaquedir->d_inode);
409 	err = ovl_set_attr(ofs, opaquedir, &stat);
410 	inode_unlock(opaquedir->d_inode);
411 	if (err)
412 		goto out_cleanup;
413 
414 	err = ovl_do_rename(ofs, wdir, opaquedir, udir, upper, RENAME_EXCHANGE);
415 	if (err)
416 		goto out_cleanup;
417 
418 	ovl_cleanup_whiteouts(ofs, upper, list);
419 	ovl_cleanup(ofs, wdir, upper);
420 	unlock_rename(workdir, upperdir);
421 
422 	/* dentry's upper doesn't match now, get rid of it */
423 	d_drop(dentry);
424 
425 	return opaquedir;
426 
427 out_cleanup:
428 	ovl_cleanup(ofs, wdir, opaquedir);
429 	dput(opaquedir);
430 out_unlock:
431 	unlock_rename(workdir, upperdir);
432 out:
433 	return ERR_PTR(err);
434 }
435 
436 static int ovl_set_upper_acl(struct ovl_fs *ofs, struct dentry *upperdentry,
437 			     const char *acl_name, struct posix_acl *acl)
438 {
439 	if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !acl)
440 		return 0;
441 
442 	return ovl_do_set_acl(ofs, upperdentry, acl_name, acl);
443 }
444 
445 static int ovl_create_over_whiteout(struct dentry *dentry, struct inode *inode,
446 				    struct ovl_cattr *cattr)
447 {
448 	struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
449 	struct dentry *workdir = ovl_workdir(dentry);
450 	struct inode *wdir = workdir->d_inode;
451 	struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
452 	struct inode *udir = upperdir->d_inode;
453 	struct dentry *upper;
454 	struct dentry *newdentry;
455 	int err;
456 	struct posix_acl *acl, *default_acl;
457 	bool hardlink = !!cattr->hardlink;
458 
459 	if (WARN_ON(!workdir))
460 		return -EROFS;
461 
462 	if (!hardlink) {
463 		err = posix_acl_create(dentry->d_parent->d_inode,
464 				       &cattr->mode, &default_acl, &acl);
465 		if (err)
466 			return err;
467 	}
468 
469 	err = ovl_lock_rename_workdir(workdir, upperdir);
470 	if (err)
471 		goto out;
472 
473 	upper = ovl_lookup_upper(ofs, dentry->d_name.name, upperdir,
474 				 dentry->d_name.len);
475 	err = PTR_ERR(upper);
476 	if (IS_ERR(upper))
477 		goto out_unlock;
478 
479 	err = -ESTALE;
480 	if (d_is_negative(upper) || !ovl_upper_is_whiteout(ofs, upper))
481 		goto out_dput;
482 
483 	newdentry = ovl_create_temp(ofs, workdir, cattr);
484 	err = PTR_ERR(newdentry);
485 	if (IS_ERR(newdentry))
486 		goto out_dput;
487 
488 	/*
489 	 * mode could have been mutilated due to umask (e.g. sgid directory)
490 	 */
491 	if (!hardlink &&
492 	    !S_ISLNK(cattr->mode) &&
493 	    newdentry->d_inode->i_mode != cattr->mode) {
494 		struct iattr attr = {
495 			.ia_valid = ATTR_MODE,
496 			.ia_mode = cattr->mode,
497 		};
498 		inode_lock(newdentry->d_inode);
499 		err = ovl_do_notify_change(ofs, newdentry, &attr);
500 		inode_unlock(newdentry->d_inode);
501 		if (err)
502 			goto out_cleanup;
503 	}
504 	if (!hardlink) {
505 		err = ovl_set_upper_acl(ofs, newdentry,
506 					XATTR_NAME_POSIX_ACL_ACCESS, acl);
507 		if (err)
508 			goto out_cleanup;
509 
510 		err = ovl_set_upper_acl(ofs, newdentry,
511 					XATTR_NAME_POSIX_ACL_DEFAULT, default_acl);
512 		if (err)
513 			goto out_cleanup;
514 	}
515 
516 	if (!hardlink && S_ISDIR(cattr->mode)) {
517 		err = ovl_set_opaque(dentry, newdentry);
518 		if (err)
519 			goto out_cleanup;
520 
521 		err = ovl_do_rename(ofs, wdir, newdentry, udir, upper,
522 				    RENAME_EXCHANGE);
523 		if (err)
524 			goto out_cleanup;
525 
526 		ovl_cleanup(ofs, wdir, upper);
527 	} else {
528 		err = ovl_do_rename(ofs, wdir, newdentry, udir, upper, 0);
529 		if (err)
530 			goto out_cleanup;
531 	}
532 	err = ovl_instantiate(dentry, inode, newdentry, hardlink);
533 	if (err) {
534 		ovl_cleanup(ofs, udir, newdentry);
535 		dput(newdentry);
536 	}
537 out_dput:
538 	dput(upper);
539 out_unlock:
540 	unlock_rename(workdir, upperdir);
541 out:
542 	if (!hardlink) {
543 		posix_acl_release(acl);
544 		posix_acl_release(default_acl);
545 	}
546 	return err;
547 
548 out_cleanup:
549 	ovl_cleanup(ofs, wdir, newdentry);
550 	dput(newdentry);
551 	goto out_dput;
552 }
553 
554 static int ovl_create_or_link(struct dentry *dentry, struct inode *inode,
555 			      struct ovl_cattr *attr, bool origin)
556 {
557 	int err;
558 	const struct cred *old_cred;
559 	struct cred *override_cred;
560 	struct dentry *parent = dentry->d_parent;
561 
562 	old_cred = ovl_override_creds(dentry->d_sb);
563 
564 	/*
565 	 * When linking a file with copy up origin into a new parent, mark the
566 	 * new parent dir "impure".
567 	 */
568 	if (origin) {
569 		err = ovl_set_impure(parent, ovl_dentry_upper(parent));
570 		if (err)
571 			goto out_revert_creds;
572 	}
573 
574 	if (!attr->hardlink) {
575 		err = -ENOMEM;
576 		override_cred = prepare_creds();
577 		if (!override_cred)
578 			goto out_revert_creds;
579 		/*
580 		 * In the creation cases(create, mkdir, mknod, symlink),
581 		 * ovl should transfer current's fs{u,g}id to underlying
582 		 * fs. Because underlying fs want to initialize its new
583 		 * inode owner using current's fs{u,g}id. And in this
584 		 * case, the @inode is a new inode that is initialized
585 		 * in inode_init_owner() to current's fs{u,g}id. So use
586 		 * the inode's i_{u,g}id to override the cred's fs{u,g}id.
587 		 *
588 		 * But in the other hardlink case, ovl_link() does not
589 		 * create a new inode, so just use the ovl mounter's
590 		 * fs{u,g}id.
591 		 */
592 		override_cred->fsuid = inode->i_uid;
593 		override_cred->fsgid = inode->i_gid;
594 		err = security_dentry_create_files_as(dentry,
595 				attr->mode, &dentry->d_name, old_cred,
596 				override_cred);
597 		if (err) {
598 			put_cred(override_cred);
599 			goto out_revert_creds;
600 		}
601 		put_cred(override_creds(override_cred));
602 		put_cred(override_cred);
603 	}
604 
605 	if (!ovl_dentry_is_whiteout(dentry))
606 		err = ovl_create_upper(dentry, inode, attr);
607 	else
608 		err = ovl_create_over_whiteout(dentry, inode, attr);
609 
610 out_revert_creds:
611 	revert_creds(old_cred);
612 	return err;
613 }
614 
615 static int ovl_create_object(struct dentry *dentry, int mode, dev_t rdev,
616 			     const char *link)
617 {
618 	int err;
619 	struct inode *inode;
620 	struct ovl_cattr attr = {
621 		.rdev = rdev,
622 		.link = link,
623 	};
624 
625 	err = ovl_copy_up(dentry->d_parent);
626 	if (err)
627 		return err;
628 
629 	err = ovl_want_write(dentry);
630 	if (err)
631 		goto out;
632 
633 	/* Preallocate inode to be used by ovl_get_inode() */
634 	err = -ENOMEM;
635 	inode = ovl_new_inode(dentry->d_sb, mode, rdev);
636 	if (!inode)
637 		goto out_drop_write;
638 
639 	spin_lock(&inode->i_lock);
640 	inode->i_state |= I_CREATING;
641 	spin_unlock(&inode->i_lock);
642 
643 	inode_init_owner(&nop_mnt_idmap, inode, dentry->d_parent->d_inode, mode);
644 	attr.mode = inode->i_mode;
645 
646 	err = ovl_create_or_link(dentry, inode, &attr, false);
647 	/* Did we end up using the preallocated inode? */
648 	if (inode != d_inode(dentry))
649 		iput(inode);
650 
651 out_drop_write:
652 	ovl_drop_write(dentry);
653 out:
654 	return err;
655 }
656 
657 static int ovl_create(struct mnt_idmap *idmap, struct inode *dir,
658 		      struct dentry *dentry, umode_t mode, bool excl)
659 {
660 	return ovl_create_object(dentry, (mode & 07777) | S_IFREG, 0, NULL);
661 }
662 
663 static int ovl_mkdir(struct mnt_idmap *idmap, struct inode *dir,
664 		     struct dentry *dentry, umode_t mode)
665 {
666 	return ovl_create_object(dentry, (mode & 07777) | S_IFDIR, 0, NULL);
667 }
668 
669 static int ovl_mknod(struct mnt_idmap *idmap, struct inode *dir,
670 		     struct dentry *dentry, umode_t mode, dev_t rdev)
671 {
672 	/* Don't allow creation of "whiteout" on overlay */
673 	if (S_ISCHR(mode) && rdev == WHITEOUT_DEV)
674 		return -EPERM;
675 
676 	return ovl_create_object(dentry, mode, rdev, NULL);
677 }
678 
679 static int ovl_symlink(struct mnt_idmap *idmap, struct inode *dir,
680 		       struct dentry *dentry, const char *link)
681 {
682 	return ovl_create_object(dentry, S_IFLNK, 0, link);
683 }
684 
685 static int ovl_set_link_redirect(struct dentry *dentry)
686 {
687 	const struct cred *old_cred;
688 	int err;
689 
690 	old_cred = ovl_override_creds(dentry->d_sb);
691 	err = ovl_set_redirect(dentry, false);
692 	revert_creds(old_cred);
693 
694 	return err;
695 }
696 
697 static int ovl_link(struct dentry *old, struct inode *newdir,
698 		    struct dentry *new)
699 {
700 	int err;
701 	struct inode *inode;
702 
703 	err = ovl_copy_up(old);
704 	if (err)
705 		goto out;
706 
707 	err = ovl_copy_up(new->d_parent);
708 	if (err)
709 		goto out;
710 
711 	err = ovl_nlink_start(old);
712 	if (err)
713 		goto out;
714 
715 	if (ovl_is_metacopy_dentry(old)) {
716 		err = ovl_set_link_redirect(old);
717 		if (err)
718 			goto out_nlink_end;
719 	}
720 
721 	inode = d_inode(old);
722 	ihold(inode);
723 
724 	err = ovl_create_or_link(new, inode,
725 			&(struct ovl_cattr) {.hardlink = ovl_dentry_upper(old)},
726 			ovl_type_origin(old));
727 	if (err)
728 		iput(inode);
729 
730 out_nlink_end:
731 	ovl_nlink_end(old);
732 out:
733 	return err;
734 }
735 
736 static bool ovl_matches_upper(struct dentry *dentry, struct dentry *upper)
737 {
738 	return d_inode(ovl_dentry_upper(dentry)) == d_inode(upper);
739 }
740 
741 static int ovl_remove_and_whiteout(struct dentry *dentry,
742 				   struct list_head *list)
743 {
744 	struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
745 	struct dentry *workdir = ovl_workdir(dentry);
746 	struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
747 	struct dentry *upper;
748 	struct dentry *opaquedir = NULL;
749 	int err;
750 
751 	if (WARN_ON(!workdir))
752 		return -EROFS;
753 
754 	if (!list_empty(list)) {
755 		opaquedir = ovl_clear_empty(dentry, list);
756 		err = PTR_ERR(opaquedir);
757 		if (IS_ERR(opaquedir))
758 			goto out;
759 	}
760 
761 	err = ovl_lock_rename_workdir(workdir, upperdir);
762 	if (err)
763 		goto out_dput;
764 
765 	upper = ovl_lookup_upper(ofs, dentry->d_name.name, upperdir,
766 				 dentry->d_name.len);
767 	err = PTR_ERR(upper);
768 	if (IS_ERR(upper))
769 		goto out_unlock;
770 
771 	err = -ESTALE;
772 	if ((opaquedir && upper != opaquedir) ||
773 	    (!opaquedir && ovl_dentry_upper(dentry) &&
774 	     !ovl_matches_upper(dentry, upper))) {
775 		goto out_dput_upper;
776 	}
777 
778 	err = ovl_cleanup_and_whiteout(ofs, d_inode(upperdir), upper);
779 	if (err)
780 		goto out_d_drop;
781 
782 	ovl_dir_modified(dentry->d_parent, true);
783 out_d_drop:
784 	d_drop(dentry);
785 out_dput_upper:
786 	dput(upper);
787 out_unlock:
788 	unlock_rename(workdir, upperdir);
789 out_dput:
790 	dput(opaquedir);
791 out:
792 	return err;
793 }
794 
795 static int ovl_remove_upper(struct dentry *dentry, bool is_dir,
796 			    struct list_head *list)
797 {
798 	struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
799 	struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
800 	struct inode *dir = upperdir->d_inode;
801 	struct dentry *upper;
802 	struct dentry *opaquedir = NULL;
803 	int err;
804 
805 	if (!list_empty(list)) {
806 		opaquedir = ovl_clear_empty(dentry, list);
807 		err = PTR_ERR(opaquedir);
808 		if (IS_ERR(opaquedir))
809 			goto out;
810 	}
811 
812 	inode_lock_nested(dir, I_MUTEX_PARENT);
813 	upper = ovl_lookup_upper(ofs, dentry->d_name.name, upperdir,
814 				 dentry->d_name.len);
815 	err = PTR_ERR(upper);
816 	if (IS_ERR(upper))
817 		goto out_unlock;
818 
819 	err = -ESTALE;
820 	if ((opaquedir && upper != opaquedir) ||
821 	    (!opaquedir && !ovl_matches_upper(dentry, upper)))
822 		goto out_dput_upper;
823 
824 	if (is_dir)
825 		err = ovl_do_rmdir(ofs, dir, upper);
826 	else
827 		err = ovl_do_unlink(ofs, dir, upper);
828 	ovl_dir_modified(dentry->d_parent, ovl_type_origin(dentry));
829 
830 	/*
831 	 * Keeping this dentry hashed would mean having to release
832 	 * upperpath/lowerpath, which could only be done if we are the
833 	 * sole user of this dentry.  Too tricky...  Just unhash for
834 	 * now.
835 	 */
836 	if (!err)
837 		d_drop(dentry);
838 out_dput_upper:
839 	dput(upper);
840 out_unlock:
841 	inode_unlock(dir);
842 	dput(opaquedir);
843 out:
844 	return err;
845 }
846 
847 static bool ovl_pure_upper(struct dentry *dentry)
848 {
849 	return !ovl_dentry_lower(dentry) &&
850 	       !ovl_test_flag(OVL_WHITEOUTS, d_inode(dentry));
851 }
852 
853 static void ovl_drop_nlink(struct dentry *dentry)
854 {
855 	struct inode *inode = d_inode(dentry);
856 	struct dentry *alias;
857 
858 	/* Try to find another, hashed alias */
859 	spin_lock(&inode->i_lock);
860 	hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
861 		if (alias != dentry && !d_unhashed(alias))
862 			break;
863 	}
864 	spin_unlock(&inode->i_lock);
865 
866 	/*
867 	 * Changes to underlying layers may cause i_nlink to lose sync with
868 	 * reality.  In this case prevent the link count from going to zero
869 	 * prematurely.
870 	 */
871 	if (inode->i_nlink > !!alias)
872 		drop_nlink(inode);
873 }
874 
875 static int ovl_do_remove(struct dentry *dentry, bool is_dir)
876 {
877 	int err;
878 	const struct cred *old_cred;
879 	bool lower_positive = ovl_lower_positive(dentry);
880 	LIST_HEAD(list);
881 
882 	/* No need to clean pure upper removed by vfs_rmdir() */
883 	if (is_dir && (lower_positive || !ovl_pure_upper(dentry))) {
884 		err = ovl_check_empty_dir(dentry, &list);
885 		if (err)
886 			goto out;
887 	}
888 
889 	err = ovl_copy_up(dentry->d_parent);
890 	if (err)
891 		goto out;
892 
893 	err = ovl_nlink_start(dentry);
894 	if (err)
895 		goto out;
896 
897 	old_cred = ovl_override_creds(dentry->d_sb);
898 	if (!lower_positive)
899 		err = ovl_remove_upper(dentry, is_dir, &list);
900 	else
901 		err = ovl_remove_and_whiteout(dentry, &list);
902 	revert_creds(old_cred);
903 	if (!err) {
904 		if (is_dir)
905 			clear_nlink(dentry->d_inode);
906 		else
907 			ovl_drop_nlink(dentry);
908 	}
909 	ovl_nlink_end(dentry);
910 
911 	/*
912 	 * Copy ctime
913 	 *
914 	 * Note: we fail to update ctime if there was no copy-up, only a
915 	 * whiteout
916 	 */
917 	if (ovl_dentry_upper(dentry))
918 		ovl_copyattr(d_inode(dentry));
919 
920 out:
921 	ovl_cache_free(&list);
922 	return err;
923 }
924 
925 static int ovl_unlink(struct inode *dir, struct dentry *dentry)
926 {
927 	return ovl_do_remove(dentry, false);
928 }
929 
930 static int ovl_rmdir(struct inode *dir, struct dentry *dentry)
931 {
932 	return ovl_do_remove(dentry, true);
933 }
934 
935 static bool ovl_type_merge_or_lower(struct dentry *dentry)
936 {
937 	enum ovl_path_type type = ovl_path_type(dentry);
938 
939 	return OVL_TYPE_MERGE(type) || !OVL_TYPE_UPPER(type);
940 }
941 
942 static bool ovl_can_move(struct dentry *dentry)
943 {
944 	return ovl_redirect_dir(OVL_FS(dentry->d_sb)) ||
945 		!d_is_dir(dentry) || !ovl_type_merge_or_lower(dentry);
946 }
947 
948 static char *ovl_get_redirect(struct dentry *dentry, bool abs_redirect)
949 {
950 	char *buf, *ret;
951 	struct dentry *d, *tmp;
952 	int buflen = ovl_redirect_max + 1;
953 
954 	if (!abs_redirect) {
955 		ret = kstrndup(dentry->d_name.name, dentry->d_name.len,
956 			       GFP_KERNEL);
957 		goto out;
958 	}
959 
960 	buf = ret = kmalloc(buflen, GFP_KERNEL);
961 	if (!buf)
962 		goto out;
963 
964 	buflen--;
965 	buf[buflen] = '\0';
966 	for (d = dget(dentry); !IS_ROOT(d);) {
967 		const char *name;
968 		int thislen;
969 
970 		spin_lock(&d->d_lock);
971 		name = ovl_dentry_get_redirect(d);
972 		if (name) {
973 			thislen = strlen(name);
974 		} else {
975 			name = d->d_name.name;
976 			thislen = d->d_name.len;
977 		}
978 
979 		/* If path is too long, fall back to userspace move */
980 		if (thislen + (name[0] != '/') > buflen) {
981 			ret = ERR_PTR(-EXDEV);
982 			spin_unlock(&d->d_lock);
983 			goto out_put;
984 		}
985 
986 		buflen -= thislen;
987 		memcpy(&buf[buflen], name, thislen);
988 		spin_unlock(&d->d_lock);
989 		tmp = dget_parent(d);
990 
991 		dput(d);
992 		d = tmp;
993 
994 		/* Absolute redirect: finished */
995 		if (buf[buflen] == '/')
996 			break;
997 		buflen--;
998 		buf[buflen] = '/';
999 	}
1000 	ret = kstrdup(&buf[buflen], GFP_KERNEL);
1001 out_put:
1002 	dput(d);
1003 	kfree(buf);
1004 out:
1005 	return ret ? ret : ERR_PTR(-ENOMEM);
1006 }
1007 
1008 static bool ovl_need_absolute_redirect(struct dentry *dentry, bool samedir)
1009 {
1010 	struct dentry *lowerdentry;
1011 
1012 	if (!samedir)
1013 		return true;
1014 
1015 	if (d_is_dir(dentry))
1016 		return false;
1017 
1018 	/*
1019 	 * For non-dir hardlinked files, we need absolute redirects
1020 	 * in general as two upper hardlinks could be in different
1021 	 * dirs. We could put a relative redirect now and convert
1022 	 * it to absolute redirect later. But when nlink > 1 and
1023 	 * indexing is on, that means relative redirect needs to be
1024 	 * converted to absolute during copy up of another lower
1025 	 * hardllink as well.
1026 	 *
1027 	 * So without optimizing too much, just check if lower is
1028 	 * a hard link or not. If lower is hard link, put absolute
1029 	 * redirect.
1030 	 */
1031 	lowerdentry = ovl_dentry_lower(dentry);
1032 	return (d_inode(lowerdentry)->i_nlink > 1);
1033 }
1034 
1035 static int ovl_set_redirect(struct dentry *dentry, bool samedir)
1036 {
1037 	int err;
1038 	struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
1039 	const char *redirect = ovl_dentry_get_redirect(dentry);
1040 	bool absolute_redirect = ovl_need_absolute_redirect(dentry, samedir);
1041 
1042 	if (redirect && (!absolute_redirect || redirect[0] == '/'))
1043 		return 0;
1044 
1045 	redirect = ovl_get_redirect(dentry, absolute_redirect);
1046 	if (IS_ERR(redirect))
1047 		return PTR_ERR(redirect);
1048 
1049 	err = ovl_check_setxattr(ofs, ovl_dentry_upper(dentry),
1050 				 OVL_XATTR_REDIRECT,
1051 				 redirect, strlen(redirect), -EXDEV);
1052 	if (!err) {
1053 		spin_lock(&dentry->d_lock);
1054 		ovl_dentry_set_redirect(dentry, redirect);
1055 		spin_unlock(&dentry->d_lock);
1056 	} else {
1057 		kfree(redirect);
1058 		pr_warn_ratelimited("failed to set redirect (%i)\n",
1059 				    err);
1060 		/* Fall back to userspace copy-up */
1061 		err = -EXDEV;
1062 	}
1063 	return err;
1064 }
1065 
1066 static int ovl_rename(struct mnt_idmap *idmap, struct inode *olddir,
1067 		      struct dentry *old, struct inode *newdir,
1068 		      struct dentry *new, unsigned int flags)
1069 {
1070 	int err;
1071 	struct dentry *old_upperdir;
1072 	struct dentry *new_upperdir;
1073 	struct dentry *olddentry;
1074 	struct dentry *newdentry;
1075 	struct dentry *trap;
1076 	bool old_opaque;
1077 	bool new_opaque;
1078 	bool cleanup_whiteout = false;
1079 	bool update_nlink = false;
1080 	bool overwrite = !(flags & RENAME_EXCHANGE);
1081 	bool is_dir = d_is_dir(old);
1082 	bool new_is_dir = d_is_dir(new);
1083 	bool samedir = olddir == newdir;
1084 	struct dentry *opaquedir = NULL;
1085 	const struct cred *old_cred = NULL;
1086 	struct ovl_fs *ofs = OVL_FS(old->d_sb);
1087 	LIST_HEAD(list);
1088 
1089 	err = -EINVAL;
1090 	if (flags & ~(RENAME_EXCHANGE | RENAME_NOREPLACE))
1091 		goto out;
1092 
1093 	flags &= ~RENAME_NOREPLACE;
1094 
1095 	/* Don't copy up directory trees */
1096 	err = -EXDEV;
1097 	if (!ovl_can_move(old))
1098 		goto out;
1099 	if (!overwrite && !ovl_can_move(new))
1100 		goto out;
1101 
1102 	if (overwrite && new_is_dir && !ovl_pure_upper(new)) {
1103 		err = ovl_check_empty_dir(new, &list);
1104 		if (err)
1105 			goto out;
1106 	}
1107 
1108 	if (overwrite) {
1109 		if (ovl_lower_positive(old)) {
1110 			if (!ovl_dentry_is_whiteout(new)) {
1111 				/* Whiteout source */
1112 				flags |= RENAME_WHITEOUT;
1113 			} else {
1114 				/* Switch whiteouts */
1115 				flags |= RENAME_EXCHANGE;
1116 			}
1117 		} else if (is_dir && ovl_dentry_is_whiteout(new)) {
1118 			flags |= RENAME_EXCHANGE;
1119 			cleanup_whiteout = true;
1120 		}
1121 	}
1122 
1123 	err = ovl_copy_up(old);
1124 	if (err)
1125 		goto out;
1126 
1127 	err = ovl_copy_up(new->d_parent);
1128 	if (err)
1129 		goto out;
1130 	if (!overwrite) {
1131 		err = ovl_copy_up(new);
1132 		if (err)
1133 			goto out;
1134 	} else if (d_inode(new)) {
1135 		err = ovl_nlink_start(new);
1136 		if (err)
1137 			goto out;
1138 
1139 		update_nlink = true;
1140 	}
1141 
1142 	if (!update_nlink) {
1143 		/* ovl_nlink_start() took ovl_want_write() */
1144 		err = ovl_want_write(old);
1145 		if (err)
1146 			goto out;
1147 	}
1148 
1149 	old_cred = ovl_override_creds(old->d_sb);
1150 
1151 	if (!list_empty(&list)) {
1152 		opaquedir = ovl_clear_empty(new, &list);
1153 		err = PTR_ERR(opaquedir);
1154 		if (IS_ERR(opaquedir)) {
1155 			opaquedir = NULL;
1156 			goto out_revert_creds;
1157 		}
1158 	}
1159 
1160 	old_upperdir = ovl_dentry_upper(old->d_parent);
1161 	new_upperdir = ovl_dentry_upper(new->d_parent);
1162 
1163 	if (!samedir) {
1164 		/*
1165 		 * When moving a merge dir or non-dir with copy up origin into
1166 		 * a new parent, we are marking the new parent dir "impure".
1167 		 * When ovl_iterate() iterates an "impure" upper dir, it will
1168 		 * lookup the origin inodes of the entries to fill d_ino.
1169 		 */
1170 		if (ovl_type_origin(old)) {
1171 			err = ovl_set_impure(new->d_parent, new_upperdir);
1172 			if (err)
1173 				goto out_revert_creds;
1174 		}
1175 		if (!overwrite && ovl_type_origin(new)) {
1176 			err = ovl_set_impure(old->d_parent, old_upperdir);
1177 			if (err)
1178 				goto out_revert_creds;
1179 		}
1180 	}
1181 
1182 	trap = lock_rename(new_upperdir, old_upperdir);
1183 	if (IS_ERR(trap)) {
1184 		err = PTR_ERR(trap);
1185 		goto out_revert_creds;
1186 	}
1187 
1188 	olddentry = ovl_lookup_upper(ofs, old->d_name.name, old_upperdir,
1189 				     old->d_name.len);
1190 	err = PTR_ERR(olddentry);
1191 	if (IS_ERR(olddentry))
1192 		goto out_unlock;
1193 
1194 	err = -ESTALE;
1195 	if (!ovl_matches_upper(old, olddentry))
1196 		goto out_dput_old;
1197 
1198 	newdentry = ovl_lookup_upper(ofs, new->d_name.name, new_upperdir,
1199 				     new->d_name.len);
1200 	err = PTR_ERR(newdentry);
1201 	if (IS_ERR(newdentry))
1202 		goto out_dput_old;
1203 
1204 	old_opaque = ovl_dentry_is_opaque(old);
1205 	new_opaque = ovl_dentry_is_opaque(new);
1206 
1207 	err = -ESTALE;
1208 	if (d_inode(new) && ovl_dentry_upper(new)) {
1209 		if (opaquedir) {
1210 			if (newdentry != opaquedir)
1211 				goto out_dput;
1212 		} else {
1213 			if (!ovl_matches_upper(new, newdentry))
1214 				goto out_dput;
1215 		}
1216 	} else {
1217 		if (!d_is_negative(newdentry)) {
1218 			if (!new_opaque || !ovl_upper_is_whiteout(ofs, newdentry))
1219 				goto out_dput;
1220 		} else {
1221 			if (flags & RENAME_EXCHANGE)
1222 				goto out_dput;
1223 		}
1224 	}
1225 
1226 	if (olddentry == trap)
1227 		goto out_dput;
1228 	if (newdentry == trap)
1229 		goto out_dput;
1230 
1231 	if (olddentry->d_inode == newdentry->d_inode)
1232 		goto out_dput;
1233 
1234 	err = 0;
1235 	if (ovl_type_merge_or_lower(old))
1236 		err = ovl_set_redirect(old, samedir);
1237 	else if (is_dir && !old_opaque && ovl_type_merge(new->d_parent))
1238 		err = ovl_set_opaque_xerr(old, olddentry, -EXDEV);
1239 	if (err)
1240 		goto out_dput;
1241 
1242 	if (!overwrite && ovl_type_merge_or_lower(new))
1243 		err = ovl_set_redirect(new, samedir);
1244 	else if (!overwrite && new_is_dir && !new_opaque &&
1245 		 ovl_type_merge(old->d_parent))
1246 		err = ovl_set_opaque_xerr(new, newdentry, -EXDEV);
1247 	if (err)
1248 		goto out_dput;
1249 
1250 	err = ovl_do_rename(ofs, old_upperdir->d_inode, olddentry,
1251 			    new_upperdir->d_inode, newdentry, flags);
1252 	if (err)
1253 		goto out_dput;
1254 
1255 	if (cleanup_whiteout)
1256 		ovl_cleanup(ofs, old_upperdir->d_inode, newdentry);
1257 
1258 	if (overwrite && d_inode(new)) {
1259 		if (new_is_dir)
1260 			clear_nlink(d_inode(new));
1261 		else
1262 			ovl_drop_nlink(new);
1263 	}
1264 
1265 	ovl_dir_modified(old->d_parent, ovl_type_origin(old) ||
1266 			 (!overwrite && ovl_type_origin(new)));
1267 	ovl_dir_modified(new->d_parent, ovl_type_origin(old) ||
1268 			 (d_inode(new) && ovl_type_origin(new)));
1269 
1270 	/* copy ctime: */
1271 	ovl_copyattr(d_inode(old));
1272 	if (d_inode(new) && ovl_dentry_upper(new))
1273 		ovl_copyattr(d_inode(new));
1274 
1275 out_dput:
1276 	dput(newdentry);
1277 out_dput_old:
1278 	dput(olddentry);
1279 out_unlock:
1280 	unlock_rename(new_upperdir, old_upperdir);
1281 out_revert_creds:
1282 	revert_creds(old_cred);
1283 	if (update_nlink)
1284 		ovl_nlink_end(new);
1285 	else
1286 		ovl_drop_write(old);
1287 out:
1288 	dput(opaquedir);
1289 	ovl_cache_free(&list);
1290 	return err;
1291 }
1292 
1293 const struct inode_operations ovl_dir_inode_operations = {
1294 	.lookup		= ovl_lookup,
1295 	.mkdir		= ovl_mkdir,
1296 	.symlink	= ovl_symlink,
1297 	.unlink		= ovl_unlink,
1298 	.rmdir		= ovl_rmdir,
1299 	.rename		= ovl_rename,
1300 	.link		= ovl_link,
1301 	.setattr	= ovl_setattr,
1302 	.create		= ovl_create,
1303 	.mknod		= ovl_mknod,
1304 	.permission	= ovl_permission,
1305 	.getattr	= ovl_getattr,
1306 	.listxattr	= ovl_listxattr,
1307 	.get_inode_acl	= ovl_get_inode_acl,
1308 	.get_acl	= ovl_get_acl,
1309 	.set_acl	= ovl_set_acl,
1310 	.update_time	= ovl_update_time,
1311 	.fileattr_get	= ovl_fileattr_get,
1312 	.fileattr_set	= ovl_fileattr_set,
1313 };
1314