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