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