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