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