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