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