1 // SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (C) 2011 Novell Inc.
7 #include <linux/module.h>
9 #include <linux/slab.h>
10 #include <linux/file.h>
11 #include <linux/splice.h>
12 #include <linux/xattr.h>
13 #include <linux/security.h>
14 #include <linux/uaccess.h>
15 #include <linux/sched/signal.h>
16 #include <linux/cred.h>
17 #include <linux/namei.h>
18 #include <linux/fdtable.h>
19 #include <linux/ratelimit.h>
20 #include <linux/exportfs.h>
21 #include "overlayfs.h"
23 #define OVL_COPY_UP_CHUNK_SIZE (1 << 20)
25 static int ovl_ccup_set(const char *buf, const struct kernel_param *param)
27 pr_warn("overlayfs: \"check_copy_up\" module option is obsolete\n");
31 static int ovl_ccup_get(char *buf, const struct kernel_param *param)
33 return sprintf(buf, "N\n");
36 module_param_call(check_copy_up, ovl_ccup_set, ovl_ccup_get, NULL, 0644);
37 MODULE_PARM_DESC(check_copy_up, "Obsolete; does nothing");
39 int ovl_copy_xattr(struct dentry *old, struct dentry *new)
41 ssize_t list_size, size, value_size = 0;
42 char *buf, *name, *value = NULL;
46 if (!(old->d_inode->i_opflags & IOP_XATTR) ||
47 !(new->d_inode->i_opflags & IOP_XATTR))
50 list_size = vfs_listxattr(old, NULL, 0);
52 if (list_size == -EOPNOTSUPP)
57 buf = kzalloc(list_size, GFP_KERNEL);
61 list_size = vfs_listxattr(old, buf, list_size);
67 for (name = buf; list_size; name += slen) {
68 slen = strnlen(name, list_size) + 1;
70 /* underlying fs providing us with an broken xattr list? */
71 if (WARN_ON(slen > list_size)) {
77 if (ovl_is_private_xattr(name))
80 error = security_inode_copy_up_xattr(name);
81 if (error < 0 && error != -EOPNOTSUPP)
85 continue; /* Discard */
88 size = vfs_getxattr(old, name, value, value_size);
90 size = vfs_getxattr(old, name, NULL, 0);
97 if (size > value_size) {
100 new = krealloc(value, size, GFP_KERNEL);
110 error = vfs_setxattr(new, name, value, size, 0);
120 static int ovl_copy_up_data(struct path *old, struct path *new, loff_t len)
122 struct file *old_file;
123 struct file *new_file;
132 old_file = ovl_path_open(old, O_LARGEFILE | O_RDONLY);
133 if (IS_ERR(old_file))
134 return PTR_ERR(old_file);
136 new_file = ovl_path_open(new, O_LARGEFILE | O_WRONLY);
137 if (IS_ERR(new_file)) {
138 error = PTR_ERR(new_file);
142 /* Try to use clone_file_range to clone up within the same fs */
143 cloned = do_clone_file_range(old_file, 0, new_file, 0, len, 0);
146 /* Couldn't clone, so now we try to copy the data */
148 /* FIXME: copy up sparse files efficiently */
150 size_t this_len = OVL_COPY_UP_CHUNK_SIZE;
156 if (signal_pending_state(TASK_KILLABLE, current)) {
161 bytes = do_splice_direct(old_file, &old_pos,
163 this_len, SPLICE_F_MOVE);
168 WARN_ON(old_pos != new_pos);
174 error = vfs_fsync(new_file, 0);
181 static int ovl_set_size(struct dentry *upperdentry, struct kstat *stat)
183 struct iattr attr = {
184 .ia_valid = ATTR_SIZE,
185 .ia_size = stat->size,
188 return notify_change(upperdentry, &attr, NULL);
191 static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat)
193 struct iattr attr = {
195 ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET,
196 .ia_atime = stat->atime,
197 .ia_mtime = stat->mtime,
200 return notify_change(upperdentry, &attr, NULL);
203 int ovl_set_attr(struct dentry *upperdentry, struct kstat *stat)
207 if (!S_ISLNK(stat->mode)) {
208 struct iattr attr = {
209 .ia_valid = ATTR_MODE,
210 .ia_mode = stat->mode,
212 err = notify_change(upperdentry, &attr, NULL);
215 struct iattr attr = {
216 .ia_valid = ATTR_UID | ATTR_GID,
220 err = notify_change(upperdentry, &attr, NULL);
223 ovl_set_timestamps(upperdentry, stat);
228 struct ovl_fh *ovl_encode_real_fh(struct dentry *real, bool is_upper)
231 int fh_type, fh_len, dwords;
233 int buflen = MAX_HANDLE_SZ;
234 uuid_t *uuid = &real->d_sb->s_uuid;
236 buf = kmalloc(buflen, GFP_KERNEL);
238 return ERR_PTR(-ENOMEM);
241 * We encode a non-connectable file handle for non-dir, because we
242 * only need to find the lower inode number and we don't want to pay
243 * the price or reconnecting the dentry.
245 dwords = buflen >> 2;
246 fh_type = exportfs_encode_fh(real, buf, &dwords, 0);
247 buflen = (dwords << 2);
250 if (WARN_ON(fh_type < 0) ||
251 WARN_ON(buflen > MAX_HANDLE_SZ) ||
252 WARN_ON(fh_type == FILEID_INVALID))
255 BUILD_BUG_ON(MAX_HANDLE_SZ + offsetof(struct ovl_fh, fid) > 255);
256 fh_len = offsetof(struct ovl_fh, fid) + buflen;
257 fh = kmalloc(fh_len, GFP_KERNEL);
259 fh = ERR_PTR(-ENOMEM);
263 fh->version = OVL_FH_VERSION;
264 fh->magic = OVL_FH_MAGIC;
266 fh->flags = OVL_FH_FLAG_CPU_ENDIAN;
268 * When we will want to decode an overlay dentry from this handle
269 * and all layers are on the same fs, if we get a disconncted real
270 * dentry when we decode fid, the only way to tell if we should assign
271 * it to upperdentry or to lowerstack is by checking this flag.
274 fh->flags |= OVL_FH_FLAG_PATH_UPPER;
277 memcpy(fh->fid, buf, buflen);
284 int ovl_set_origin(struct dentry *dentry, struct dentry *lower,
285 struct dentry *upper)
287 const struct ovl_fh *fh = NULL;
291 * When lower layer doesn't support export operations store a 'null' fh,
292 * so we can use the overlay.origin xattr to distignuish between a copy
293 * up and a pure upper inode.
295 if (ovl_can_decode_fh(lower->d_sb)) {
296 fh = ovl_encode_real_fh(lower, false);
302 * Do not fail when upper doesn't support xattrs.
304 err = ovl_check_setxattr(dentry, upper, OVL_XATTR_ORIGIN, fh,
305 fh ? fh->len : 0, 0);
311 /* Store file handle of @upper dir in @index dir entry */
312 static int ovl_set_upper_fh(struct dentry *upper, struct dentry *index)
314 const struct ovl_fh *fh;
317 fh = ovl_encode_real_fh(upper, true);
321 err = ovl_do_setxattr(index, OVL_XATTR_UPPER, fh, fh->len, 0);
328 * Create and install index entry.
330 * Caller must hold i_mutex on indexdir.
332 static int ovl_create_index(struct dentry *dentry, struct dentry *origin,
333 struct dentry *upper)
335 struct dentry *indexdir = ovl_indexdir(dentry->d_sb);
336 struct inode *dir = d_inode(indexdir);
337 struct dentry *index = NULL;
338 struct dentry *temp = NULL;
339 struct qstr name = { };
343 * For now this is only used for creating index entry for directories,
344 * because non-dir are copied up directly to index and then hardlinked
347 * TODO: implement create index for non-dir, so we can call it when
348 * encoding file handle for non-dir in case index does not exist.
350 if (WARN_ON(!d_is_dir(dentry)))
353 /* Directory not expected to be indexed before copy up */
354 if (WARN_ON(ovl_test_flag(OVL_INDEX, d_inode(dentry))))
357 err = ovl_get_index_name(origin, &name);
361 temp = ovl_create_temp(indexdir, OVL_CATTR(S_IFDIR | 0));
366 err = ovl_set_upper_fh(upper, temp);
370 index = lookup_one_len(name.name, indexdir, name.len);
372 err = PTR_ERR(index);
374 err = ovl_do_rename(dir, temp, dir, index, 0);
379 ovl_cleanup(dir, temp);
386 struct ovl_copy_up_ctx {
387 struct dentry *parent;
388 struct dentry *dentry;
389 struct path lowerpath;
393 struct dentry *destdir;
394 struct qstr destname;
395 struct dentry *workdir;
401 static int ovl_link_up(struct ovl_copy_up_ctx *c)
404 struct dentry *upper;
405 struct dentry *upperdir = ovl_dentry_upper(c->parent);
406 struct inode *udir = d_inode(upperdir);
408 /* Mark parent "impure" because it may now contain non-pure upper */
409 err = ovl_set_impure(c->parent, upperdir);
413 err = ovl_set_nlink_lower(c->dentry);
417 inode_lock_nested(udir, I_MUTEX_PARENT);
418 upper = lookup_one_len(c->dentry->d_name.name, upperdir,
419 c->dentry->d_name.len);
420 err = PTR_ERR(upper);
421 if (!IS_ERR(upper)) {
422 err = ovl_do_link(ovl_dentry_upper(c->dentry), udir, upper);
426 /* Restore timestamps on parent (best effort) */
427 ovl_set_timestamps(upperdir, &c->pstat);
428 ovl_dentry_set_upper_alias(c->dentry);
435 err = ovl_set_nlink_upper(c->dentry);
440 static int ovl_copy_up_inode(struct ovl_copy_up_ctx *c, struct dentry *temp)
445 * Copy up data first and then xattrs. Writing data after
446 * xattrs will remove security.capability xattr automatically.
448 if (S_ISREG(c->stat.mode) && !c->metacopy) {
449 struct path upperpath, datapath;
451 ovl_path_upper(c->dentry, &upperpath);
452 if (WARN_ON(upperpath.dentry != NULL))
454 upperpath.dentry = temp;
456 ovl_path_lowerdata(c->dentry, &datapath);
457 err = ovl_copy_up_data(&datapath, &upperpath, c->stat.size);
462 err = ovl_copy_xattr(c->lowerpath.dentry, temp);
467 * Store identifier of lower inode in upper inode xattr to
468 * allow lookup of the copy up origin inode.
470 * Don't set origin when we are breaking the association with a lower
474 err = ovl_set_origin(c->dentry, c->lowerpath.dentry, temp);
480 err = ovl_check_setxattr(c->dentry, temp, OVL_XATTR_METACOPY,
481 NULL, 0, -EOPNOTSUPP);
486 inode_lock(temp->d_inode);
488 err = ovl_set_size(temp, &c->stat);
490 err = ovl_set_attr(temp, &c->stat);
491 inode_unlock(temp->d_inode);
496 struct ovl_cu_creds {
497 const struct cred *old;
501 static int ovl_prep_cu_creds(struct dentry *dentry, struct ovl_cu_creds *cc)
505 cc->old = cc->new = NULL;
506 err = security_inode_copy_up(dentry, &cc->new);
511 cc->old = override_creds(cc->new);
516 static void ovl_revert_cu_creds(struct ovl_cu_creds *cc)
519 revert_creds(cc->old);
525 * Copyup using workdir to prepare temp file. Used when copying up directories,
526 * special files or when upper fs doesn't support O_TMPFILE.
528 static int ovl_copy_up_workdir(struct ovl_copy_up_ctx *c)
531 struct inode *udir = d_inode(c->destdir), *wdir = d_inode(c->workdir);
532 struct dentry *temp, *upper;
533 struct ovl_cu_creds cc;
535 struct ovl_cattr cattr = {
536 /* Can't properly set mode on creation because of the umask */
537 .mode = c->stat.mode & S_IFMT,
538 .rdev = c->stat.rdev,
542 err = ovl_lock_rename_workdir(c->workdir, c->destdir);
546 err = ovl_prep_cu_creds(c->dentry, &cc);
550 temp = ovl_create_temp(c->workdir, &cattr);
551 ovl_revert_cu_creds(&cc);
557 err = ovl_copy_up_inode(c, temp);
561 if (S_ISDIR(c->stat.mode) && c->indexed) {
562 err = ovl_create_index(c->dentry, c->lowerpath.dentry, temp);
567 upper = lookup_one_len(c->destname.name, c->destdir, c->destname.len);
568 err = PTR_ERR(upper);
572 err = ovl_do_rename(wdir, temp, udir, upper, 0);
578 ovl_set_upperdata(d_inode(c->dentry));
579 inode = d_inode(c->dentry);
580 ovl_inode_update(inode, temp);
581 if (S_ISDIR(inode->i_mode))
582 ovl_set_flag(OVL_WHITEOUTS, inode);
584 unlock_rename(c->workdir, c->destdir);
589 ovl_cleanup(wdir, temp);
594 /* Copyup using O_TMPFILE which does not require cross dir locking */
595 static int ovl_copy_up_tmpfile(struct ovl_copy_up_ctx *c)
597 struct inode *udir = d_inode(c->destdir);
598 struct dentry *temp, *upper;
599 struct ovl_cu_creds cc;
602 err = ovl_prep_cu_creds(c->dentry, &cc);
606 temp = ovl_do_tmpfile(c->workdir, c->stat.mode);
607 ovl_revert_cu_creds(&cc);
610 return PTR_ERR(temp);
612 err = ovl_copy_up_inode(c, temp);
616 inode_lock_nested(udir, I_MUTEX_PARENT);
618 upper = lookup_one_len(c->destname.name, c->destdir, c->destname.len);
619 err = PTR_ERR(upper);
620 if (!IS_ERR(upper)) {
621 err = ovl_do_link(temp, udir, upper);
630 ovl_set_upperdata(d_inode(c->dentry));
631 ovl_inode_update(d_inode(c->dentry), temp);
641 * Copy up a single dentry
643 * All renames start with copy up of source if necessary. The actual
644 * rename will only proceed once the copy up was successful. Copy up uses
645 * upper parent i_mutex for exclusion. Since rename can change d_parent it
646 * is possible that the copy up will lock the old parent. At that point
647 * the file will have already been copied up anyway.
649 static int ovl_do_copy_up(struct ovl_copy_up_ctx *c)
652 struct ovl_fs *ofs = c->dentry->d_sb->s_fs_info;
653 bool to_index = false;
656 * Indexed non-dir is copied up directly to the index entry and then
657 * hardlinked to upper dir. Indexed dir is copied up to indexdir,
658 * then index entry is created and then copied up dir installed.
659 * Copying dir up to indexdir instead of workdir simplifies locking.
661 if (ovl_need_index(c->dentry)) {
663 if (S_ISDIR(c->stat.mode))
664 c->workdir = ovl_indexdir(c->dentry->d_sb);
669 if (S_ISDIR(c->stat.mode) || c->stat.nlink == 1 || to_index)
673 c->destdir = ovl_indexdir(c->dentry->d_sb);
674 err = ovl_get_index_name(c->lowerpath.dentry, &c->destname);
677 } else if (WARN_ON(!c->parent)) {
678 /* Disconnected dentry must be copied up to index dir */
682 * Mark parent "impure" because it may now contain non-pure
685 err = ovl_set_impure(c->parent, c->destdir);
690 /* Should we copyup with O_TMPFILE or with workdir? */
691 if (S_ISREG(c->stat.mode) && ofs->tmpfile)
692 err = ovl_copy_up_tmpfile(c);
694 err = ovl_copy_up_workdir(c);
699 ovl_set_flag(OVL_INDEX, d_inode(c->dentry));
702 /* Initialize nlink for copy up of disconnected dentry */
703 err = ovl_set_nlink_upper(c->dentry);
705 struct inode *udir = d_inode(c->destdir);
707 /* Restore timestamps on parent (best effort) */
709 ovl_set_timestamps(c->destdir, &c->pstat);
712 ovl_dentry_set_upper_alias(c->dentry);
717 kfree(c->destname.name);
721 static bool ovl_need_meta_copy_up(struct dentry *dentry, umode_t mode,
724 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
726 if (!ofs->config.metacopy)
732 if (flags && ((OPEN_FMODE(flags) & FMODE_WRITE) || (flags & O_TRUNC)))
738 /* Copy up data of an inode which was copied up metadata only in the past. */
739 static int ovl_copy_up_meta_inode_data(struct ovl_copy_up_ctx *c)
741 struct path upperpath, datapath;
743 char *capability = NULL;
746 ovl_path_upper(c->dentry, &upperpath);
747 if (WARN_ON(upperpath.dentry == NULL))
750 ovl_path_lowerdata(c->dentry, &datapath);
751 if (WARN_ON(datapath.dentry == NULL))
755 err = cap_size = ovl_getxattr(upperpath.dentry, XATTR_NAME_CAPS,
757 if (err < 0 && err != -ENODATA)
761 err = ovl_copy_up_data(&datapath, &upperpath, c->stat.size);
766 * Writing to upper file will clear security.capability xattr. We
767 * don't want that to happen for normal copy-up operation.
770 err = ovl_do_setxattr(upperpath.dentry, XATTR_NAME_CAPS,
771 capability, cap_size, 0);
777 err = vfs_removexattr(upperpath.dentry, OVL_XATTR_METACOPY);
781 ovl_set_upperdata(d_inode(c->dentry));
788 static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
792 DEFINE_DELAYED_CALL(done);
793 struct path parentpath;
794 struct ovl_copy_up_ctx ctx = {
797 .workdir = ovl_workdir(dentry),
800 if (WARN_ON(!ctx.workdir))
803 ovl_path_lower(dentry, &ctx.lowerpath);
804 err = vfs_getattr(&ctx.lowerpath, &ctx.stat,
805 STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
809 ctx.metacopy = ovl_need_meta_copy_up(dentry, ctx.stat.mode, flags);
812 ovl_path_upper(parent, &parentpath);
813 ctx.destdir = parentpath.dentry;
814 ctx.destname = dentry->d_name;
816 err = vfs_getattr(&parentpath, &ctx.pstat,
817 STATX_ATIME | STATX_MTIME,
818 AT_STATX_SYNC_AS_STAT);
823 /* maybe truncate regular file. this has no effect on dirs */
827 if (S_ISLNK(ctx.stat.mode)) {
828 ctx.link = vfs_get_link(ctx.lowerpath.dentry, &done);
829 if (IS_ERR(ctx.link))
830 return PTR_ERR(ctx.link);
833 err = ovl_copy_up_start(dentry, flags);
834 /* err < 0: interrupted, err > 0: raced with another copy-up */
839 if (!ovl_dentry_upper(dentry))
840 err = ovl_do_copy_up(&ctx);
841 if (!err && parent && !ovl_dentry_has_upper_alias(dentry))
842 err = ovl_link_up(&ctx);
843 if (!err && ovl_dentry_needs_data_copy_up_locked(dentry, flags))
844 err = ovl_copy_up_meta_inode_data(&ctx);
845 ovl_copy_up_end(dentry);
847 do_delayed_call(&done);
852 int ovl_copy_up_flags(struct dentry *dentry, int flags)
855 const struct cred *old_cred;
856 bool disconnected = (dentry->d_flags & DCACHE_DISCONNECTED);
859 * With NFS export, copy up can get called for a disconnected non-dir.
860 * In this case, we will copy up lower inode to index dir without
861 * linking it to upper dir.
863 if (WARN_ON(disconnected && d_is_dir(dentry)))
866 old_cred = ovl_override_creds(dentry->d_sb);
869 struct dentry *parent = NULL;
871 if (ovl_already_copied_up(dentry, flags))
875 /* find the topmost dentry not yet copied up */
876 for (; !disconnected;) {
877 parent = dget_parent(next);
879 if (ovl_dentry_upper(parent))
886 err = ovl_copy_up_one(parent, next, flags);
891 revert_creds(old_cred);
896 static bool ovl_open_need_copy_up(struct dentry *dentry, int flags)
898 /* Copy up of disconnected dentry does not set upper alias */
899 if (ovl_already_copied_up(dentry, flags))
902 if (special_file(d_inode(dentry)->i_mode))
905 if (!ovl_open_flags_need_copy_up(flags))
911 int ovl_maybe_copy_up(struct dentry *dentry, int flags)
915 if (ovl_open_need_copy_up(dentry, flags)) {
916 err = ovl_want_write(dentry);
918 err = ovl_copy_up_flags(dentry, flags);
919 ovl_drop_write(dentry);
926 int ovl_copy_up_with_data(struct dentry *dentry)
928 return ovl_copy_up_flags(dentry, O_WRONLY);
931 int ovl_copy_up(struct dentry *dentry)
933 return ovl_copy_up_flags(dentry, 0);