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