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("\"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 static bool ovl_must_copy_xattr(const char *name)
41 return !strcmp(name, XATTR_POSIX_ACL_ACCESS) ||
42 !strcmp(name, XATTR_POSIX_ACL_DEFAULT) ||
43 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN);
46 int ovl_copy_xattr(struct super_block *sb, struct dentry *old,
49 ssize_t list_size, size, value_size = 0;
50 char *buf, *name, *value = NULL;
54 if (!(old->d_inode->i_opflags & IOP_XATTR) ||
55 !(new->d_inode->i_opflags & IOP_XATTR))
58 list_size = vfs_listxattr(old, NULL, 0);
60 if (list_size == -EOPNOTSUPP)
65 buf = kzalloc(list_size, GFP_KERNEL);
69 list_size = vfs_listxattr(old, buf, list_size);
75 for (name = buf; list_size; name += slen) {
76 slen = strnlen(name, list_size) + 1;
78 /* underlying fs providing us with an broken xattr list? */
79 if (WARN_ON(slen > list_size)) {
85 if (ovl_is_private_xattr(sb, name))
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 = security_inode_copy_up_xattr(name);
111 if (error < 0 && error != -EOPNOTSUPP)
115 continue; /* Discard */
117 error = vfs_setxattr(new, name, value, size, 0);
119 if (error != -EOPNOTSUPP || ovl_must_copy_xattr(name))
122 /* Ignore failure to copy unknown xattrs */
132 static int ovl_copy_up_data(struct ovl_fs *ofs, struct path *old,
133 struct path *new, loff_t len)
135 struct file *old_file;
136 struct file *new_file;
140 loff_t data_pos = -1;
142 bool skip_hole = false;
148 old_file = ovl_path_open(old, O_LARGEFILE | O_RDONLY);
149 if (IS_ERR(old_file))
150 return PTR_ERR(old_file);
152 new_file = ovl_path_open(new, O_LARGEFILE | O_WRONLY);
153 if (IS_ERR(new_file)) {
154 error = PTR_ERR(new_file);
158 /* Try to use clone_file_range to clone up within the same fs */
159 cloned = do_clone_file_range(old_file, 0, new_file, 0, len, 0);
162 /* Couldn't clone, so now we try to copy the data */
164 /* Check if lower fs supports seek operation */
165 if (old_file->f_mode & FMODE_LSEEK &&
166 old_file->f_op->llseek)
170 size_t this_len = OVL_COPY_UP_CHUNK_SIZE;
176 if (signal_pending_state(TASK_KILLABLE, current)) {
182 * Fill zero for hole will cost unnecessary disk space
183 * and meanwhile slow down the copy-up speed, so we do
184 * an optimization for hole during copy-up, it relies
185 * on SEEK_DATA implementation in lower fs so if lower
186 * fs does not support it, copy-up will behave as before.
188 * Detail logic of hole detection as below:
189 * When we detect next data position is larger than current
190 * position we will skip that hole, otherwise we copy
191 * data in the size of OVL_COPY_UP_CHUNK_SIZE. Actually,
192 * it may not recognize all kind of holes and sometimes
193 * only skips partial of hole area. However, it will be
194 * enough for most of the use cases.
197 if (skip_hole && data_pos < old_pos) {
198 data_pos = vfs_llseek(old_file, old_pos, SEEK_DATA);
199 if (data_pos > old_pos) {
200 hole_len = data_pos - old_pos;
202 old_pos = new_pos = data_pos;
204 } else if (data_pos == -ENXIO) {
206 } else if (data_pos < 0) {
211 bytes = do_splice_direct(old_file, &old_pos,
213 this_len, SPLICE_F_MOVE);
218 WARN_ON(old_pos != new_pos);
223 if (!error && ovl_should_sync(ofs))
224 error = vfs_fsync(new_file, 0);
231 static int ovl_set_size(struct dentry *upperdentry, struct kstat *stat)
233 struct iattr attr = {
234 .ia_valid = ATTR_SIZE,
235 .ia_size = stat->size,
238 return notify_change(upperdentry, &attr, NULL);
241 static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat)
243 struct iattr attr = {
245 ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET,
246 .ia_atime = stat->atime,
247 .ia_mtime = stat->mtime,
250 return notify_change(upperdentry, &attr, NULL);
253 int ovl_set_attr(struct dentry *upperdentry, struct kstat *stat)
257 if (!S_ISLNK(stat->mode)) {
258 struct iattr attr = {
259 .ia_valid = ATTR_MODE,
260 .ia_mode = stat->mode,
262 err = notify_change(upperdentry, &attr, NULL);
265 struct iattr attr = {
266 .ia_valid = ATTR_UID | ATTR_GID,
270 err = notify_change(upperdentry, &attr, NULL);
273 ovl_set_timestamps(upperdentry, stat);
278 struct ovl_fh *ovl_encode_real_fh(struct ovl_fs *ofs, struct dentry *real,
283 int buflen = MAX_HANDLE_SZ;
284 uuid_t *uuid = &real->d_sb->s_uuid;
287 /* Make sure the real fid stays 32bit aligned */
288 BUILD_BUG_ON(OVL_FH_FID_OFFSET % 4);
289 BUILD_BUG_ON(MAX_HANDLE_SZ + OVL_FH_FID_OFFSET > 255);
291 fh = kzalloc(buflen + OVL_FH_FID_OFFSET, GFP_KERNEL);
293 return ERR_PTR(-ENOMEM);
296 * We encode a non-connectable file handle for non-dir, because we
297 * only need to find the lower inode number and we don't want to pay
298 * the price or reconnecting the dentry.
300 dwords = buflen >> 2;
301 fh_type = exportfs_encode_fh(real, (void *)fh->fb.fid, &dwords, 0);
302 buflen = (dwords << 2);
305 if (WARN_ON(fh_type < 0) ||
306 WARN_ON(buflen > MAX_HANDLE_SZ) ||
307 WARN_ON(fh_type == FILEID_INVALID))
310 fh->fb.version = OVL_FH_VERSION;
311 fh->fb.magic = OVL_FH_MAGIC;
312 fh->fb.type = fh_type;
313 fh->fb.flags = OVL_FH_FLAG_CPU_ENDIAN;
315 * When we will want to decode an overlay dentry from this handle
316 * and all layers are on the same fs, if we get a disconncted real
317 * dentry when we decode fid, the only way to tell if we should assign
318 * it to upperdentry or to lowerstack is by checking this flag.
321 fh->fb.flags |= OVL_FH_FLAG_PATH_UPPER;
322 fh->fb.len = sizeof(fh->fb) + buflen;
323 if (ofs->config.uuid)
333 int ovl_set_origin(struct ovl_fs *ofs, struct dentry *dentry,
334 struct dentry *lower, struct dentry *upper)
336 const struct ovl_fh *fh = NULL;
340 * When lower layer doesn't support export operations store a 'null' fh,
341 * so we can use the overlay.origin xattr to distignuish between a copy
342 * up and a pure upper inode.
344 if (ovl_can_decode_fh(lower->d_sb)) {
345 fh = ovl_encode_real_fh(ofs, lower, false);
351 * Do not fail when upper doesn't support xattrs.
353 err = ovl_check_setxattr(dentry, upper, OVL_XATTR_ORIGIN, fh->buf,
354 fh ? fh->fb.len : 0, 0);
357 /* Ignore -EPERM from setting "user.*" on symlink/special */
358 return err == -EPERM ? 0 : err;
361 /* Store file handle of @upper dir in @index dir entry */
362 static int ovl_set_upper_fh(struct ovl_fs *ofs, struct dentry *upper,
363 struct dentry *index)
365 const struct ovl_fh *fh;
368 fh = ovl_encode_real_fh(ofs, upper, true);
372 err = ovl_do_setxattr(ofs, index, OVL_XATTR_UPPER, fh->buf, fh->fb.len);
379 * Create and install index entry.
381 * Caller must hold i_mutex on indexdir.
383 static int ovl_create_index(struct dentry *dentry, struct dentry *origin,
384 struct dentry *upper)
386 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
387 struct dentry *indexdir = ovl_indexdir(dentry->d_sb);
388 struct inode *dir = d_inode(indexdir);
389 struct dentry *index = NULL;
390 struct dentry *temp = NULL;
391 struct qstr name = { };
395 * For now this is only used for creating index entry for directories,
396 * because non-dir are copied up directly to index and then hardlinked
399 * TODO: implement create index for non-dir, so we can call it when
400 * encoding file handle for non-dir in case index does not exist.
402 if (WARN_ON(!d_is_dir(dentry)))
405 /* Directory not expected to be indexed before copy up */
406 if (WARN_ON(ovl_test_flag(OVL_INDEX, d_inode(dentry))))
409 err = ovl_get_index_name(ofs, origin, &name);
413 temp = ovl_create_temp(indexdir, OVL_CATTR(S_IFDIR | 0));
418 err = ovl_set_upper_fh(ofs, upper, temp);
422 index = lookup_one_len(name.name, indexdir, name.len);
424 err = PTR_ERR(index);
426 err = ovl_do_rename(dir, temp, dir, index, 0);
431 ovl_cleanup(dir, temp);
438 struct ovl_copy_up_ctx {
439 struct dentry *parent;
440 struct dentry *dentry;
441 struct path lowerpath;
445 struct dentry *destdir;
446 struct qstr destname;
447 struct dentry *workdir;
453 static int ovl_link_up(struct ovl_copy_up_ctx *c)
456 struct dentry *upper;
457 struct dentry *upperdir = ovl_dentry_upper(c->parent);
458 struct inode *udir = d_inode(upperdir);
460 /* Mark parent "impure" because it may now contain non-pure upper */
461 err = ovl_set_impure(c->parent, upperdir);
465 err = ovl_set_nlink_lower(c->dentry);
469 inode_lock_nested(udir, I_MUTEX_PARENT);
470 upper = lookup_one_len(c->dentry->d_name.name, upperdir,
471 c->dentry->d_name.len);
472 err = PTR_ERR(upper);
473 if (!IS_ERR(upper)) {
474 err = ovl_do_link(ovl_dentry_upper(c->dentry), udir, upper);
478 /* Restore timestamps on parent (best effort) */
479 ovl_set_timestamps(upperdir, &c->pstat);
480 ovl_dentry_set_upper_alias(c->dentry);
487 err = ovl_set_nlink_upper(c->dentry);
492 static int ovl_copy_up_inode(struct ovl_copy_up_ctx *c, struct dentry *temp)
494 struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb);
498 * Copy up data first and then xattrs. Writing data after
499 * xattrs will remove security.capability xattr automatically.
501 if (S_ISREG(c->stat.mode) && !c->metacopy) {
502 struct path upperpath, datapath;
504 ovl_path_upper(c->dentry, &upperpath);
505 if (WARN_ON(upperpath.dentry != NULL))
507 upperpath.dentry = temp;
509 ovl_path_lowerdata(c->dentry, &datapath);
510 err = ovl_copy_up_data(ofs, &datapath, &upperpath,
516 err = ovl_copy_xattr(c->dentry->d_sb, c->lowerpath.dentry, temp);
521 * Store identifier of lower inode in upper inode xattr to
522 * allow lookup of the copy up origin inode.
524 * Don't set origin when we are breaking the association with a lower
528 err = ovl_set_origin(ofs, c->dentry, c->lowerpath.dentry, temp);
534 err = ovl_check_setxattr(c->dentry, temp, OVL_XATTR_METACOPY,
535 NULL, 0, -EOPNOTSUPP);
540 inode_lock(temp->d_inode);
541 if (S_ISREG(c->stat.mode))
542 err = ovl_set_size(temp, &c->stat);
544 err = ovl_set_attr(temp, &c->stat);
545 inode_unlock(temp->d_inode);
550 struct ovl_cu_creds {
551 const struct cred *old;
555 static int ovl_prep_cu_creds(struct dentry *dentry, struct ovl_cu_creds *cc)
559 cc->old = cc->new = NULL;
560 err = security_inode_copy_up(dentry, &cc->new);
565 cc->old = override_creds(cc->new);
570 static void ovl_revert_cu_creds(struct ovl_cu_creds *cc)
573 revert_creds(cc->old);
579 * Copyup using workdir to prepare temp file. Used when copying up directories,
580 * special files or when upper fs doesn't support O_TMPFILE.
582 static int ovl_copy_up_workdir(struct ovl_copy_up_ctx *c)
585 struct inode *udir = d_inode(c->destdir), *wdir = d_inode(c->workdir);
586 struct dentry *temp, *upper;
587 struct ovl_cu_creds cc;
589 struct ovl_cattr cattr = {
590 /* Can't properly set mode on creation because of the umask */
591 .mode = c->stat.mode & S_IFMT,
592 .rdev = c->stat.rdev,
596 /* workdir and destdir could be the same when copying up to indexdir */
598 if (lock_rename(c->workdir, c->destdir) != NULL)
601 err = ovl_prep_cu_creds(c->dentry, &cc);
605 temp = ovl_create_temp(c->workdir, &cattr);
606 ovl_revert_cu_creds(&cc);
612 err = ovl_copy_up_inode(c, temp);
616 if (S_ISDIR(c->stat.mode) && c->indexed) {
617 err = ovl_create_index(c->dentry, c->lowerpath.dentry, temp);
622 upper = lookup_one_len(c->destname.name, c->destdir, c->destname.len);
623 err = PTR_ERR(upper);
627 err = ovl_do_rename(wdir, temp, udir, upper, 0);
633 ovl_set_upperdata(d_inode(c->dentry));
634 inode = d_inode(c->dentry);
635 ovl_inode_update(inode, temp);
636 if (S_ISDIR(inode->i_mode))
637 ovl_set_flag(OVL_WHITEOUTS, inode);
639 unlock_rename(c->workdir, c->destdir);
644 ovl_cleanup(wdir, temp);
649 /* Copyup using O_TMPFILE which does not require cross dir locking */
650 static int ovl_copy_up_tmpfile(struct ovl_copy_up_ctx *c)
652 struct inode *udir = d_inode(c->destdir);
653 struct dentry *temp, *upper;
654 struct ovl_cu_creds cc;
657 err = ovl_prep_cu_creds(c->dentry, &cc);
661 temp = ovl_do_tmpfile(c->workdir, c->stat.mode);
662 ovl_revert_cu_creds(&cc);
665 return PTR_ERR(temp);
667 err = ovl_copy_up_inode(c, temp);
671 inode_lock_nested(udir, I_MUTEX_PARENT);
673 upper = lookup_one_len(c->destname.name, c->destdir, c->destname.len);
674 err = PTR_ERR(upper);
675 if (!IS_ERR(upper)) {
676 err = ovl_do_link(temp, udir, upper);
685 ovl_set_upperdata(d_inode(c->dentry));
686 ovl_inode_update(d_inode(c->dentry), temp);
696 * Copy up a single dentry
698 * All renames start with copy up of source if necessary. The actual
699 * rename will only proceed once the copy up was successful. Copy up uses
700 * upper parent i_mutex for exclusion. Since rename can change d_parent it
701 * is possible that the copy up will lock the old parent. At that point
702 * the file will have already been copied up anyway.
704 static int ovl_do_copy_up(struct ovl_copy_up_ctx *c)
707 struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb);
708 bool to_index = false;
711 * Indexed non-dir is copied up directly to the index entry and then
712 * hardlinked to upper dir. Indexed dir is copied up to indexdir,
713 * then index entry is created and then copied up dir installed.
714 * Copying dir up to indexdir instead of workdir simplifies locking.
716 if (ovl_need_index(c->dentry)) {
718 if (S_ISDIR(c->stat.mode))
719 c->workdir = ovl_indexdir(c->dentry->d_sb);
724 if (S_ISDIR(c->stat.mode) || c->stat.nlink == 1 || to_index)
728 c->destdir = ovl_indexdir(c->dentry->d_sb);
729 err = ovl_get_index_name(ofs, c->lowerpath.dentry, &c->destname);
732 } else if (WARN_ON(!c->parent)) {
733 /* Disconnected dentry must be copied up to index dir */
737 * Mark parent "impure" because it may now contain non-pure
740 err = ovl_set_impure(c->parent, c->destdir);
745 /* Should we copyup with O_TMPFILE or with workdir? */
746 if (S_ISREG(c->stat.mode) && ofs->tmpfile)
747 err = ovl_copy_up_tmpfile(c);
749 err = ovl_copy_up_workdir(c);
754 ovl_set_flag(OVL_INDEX, d_inode(c->dentry));
757 /* Initialize nlink for copy up of disconnected dentry */
758 err = ovl_set_nlink_upper(c->dentry);
760 struct inode *udir = d_inode(c->destdir);
762 /* Restore timestamps on parent (best effort) */
764 ovl_set_timestamps(c->destdir, &c->pstat);
767 ovl_dentry_set_upper_alias(c->dentry);
772 kfree(c->destname.name);
776 static bool ovl_need_meta_copy_up(struct dentry *dentry, umode_t mode,
779 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
781 if (!ofs->config.metacopy)
787 if (flags && ((OPEN_FMODE(flags) & FMODE_WRITE) || (flags & O_TRUNC)))
793 static ssize_t ovl_getxattr(struct dentry *dentry, char *name, char **value)
798 res = vfs_getxattr(dentry, name, NULL, 0);
799 if (res == -ENODATA || res == -EOPNOTSUPP)
803 buf = kzalloc(res, GFP_KERNEL);
807 res = vfs_getxattr(dentry, name, buf, res);
816 /* Copy up data of an inode which was copied up metadata only in the past. */
817 static int ovl_copy_up_meta_inode_data(struct ovl_copy_up_ctx *c)
819 struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb);
820 struct path upperpath, datapath;
822 char *capability = NULL;
825 ovl_path_upper(c->dentry, &upperpath);
826 if (WARN_ON(upperpath.dentry == NULL))
829 ovl_path_lowerdata(c->dentry, &datapath);
830 if (WARN_ON(datapath.dentry == NULL))
834 err = cap_size = ovl_getxattr(upperpath.dentry, XATTR_NAME_CAPS,
840 err = ovl_copy_up_data(ofs, &datapath, &upperpath, c->stat.size);
845 * Writing to upper file will clear security.capability xattr. We
846 * don't want that to happen for normal copy-up operation.
849 err = vfs_setxattr(upperpath.dentry, XATTR_NAME_CAPS,
850 capability, cap_size, 0);
856 err = ovl_do_removexattr(ofs, upperpath.dentry, OVL_XATTR_METACOPY);
860 ovl_set_upperdata(d_inode(c->dentry));
867 static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
871 DEFINE_DELAYED_CALL(done);
872 struct path parentpath;
873 struct ovl_copy_up_ctx ctx = {
876 .workdir = ovl_workdir(dentry),
879 if (WARN_ON(!ctx.workdir))
882 ovl_path_lower(dentry, &ctx.lowerpath);
883 err = vfs_getattr(&ctx.lowerpath, &ctx.stat,
884 STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
888 ctx.metacopy = ovl_need_meta_copy_up(dentry, ctx.stat.mode, flags);
891 ovl_path_upper(parent, &parentpath);
892 ctx.destdir = parentpath.dentry;
893 ctx.destname = dentry->d_name;
895 err = vfs_getattr(&parentpath, &ctx.pstat,
896 STATX_ATIME | STATX_MTIME,
897 AT_STATX_SYNC_AS_STAT);
902 /* maybe truncate regular file. this has no effect on dirs */
906 if (S_ISLNK(ctx.stat.mode)) {
907 ctx.link = vfs_get_link(ctx.lowerpath.dentry, &done);
908 if (IS_ERR(ctx.link))
909 return PTR_ERR(ctx.link);
912 err = ovl_copy_up_start(dentry, flags);
913 /* err < 0: interrupted, err > 0: raced with another copy-up */
918 if (!ovl_dentry_upper(dentry))
919 err = ovl_do_copy_up(&ctx);
920 if (!err && parent && !ovl_dentry_has_upper_alias(dentry))
921 err = ovl_link_up(&ctx);
922 if (!err && ovl_dentry_needs_data_copy_up_locked(dentry, flags))
923 err = ovl_copy_up_meta_inode_data(&ctx);
924 ovl_copy_up_end(dentry);
926 do_delayed_call(&done);
931 static int ovl_copy_up_flags(struct dentry *dentry, int flags)
934 const struct cred *old_cred = ovl_override_creds(dentry->d_sb);
935 bool disconnected = (dentry->d_flags & DCACHE_DISCONNECTED);
938 * With NFS export, copy up can get called for a disconnected non-dir.
939 * In this case, we will copy up lower inode to index dir without
940 * linking it to upper dir.
942 if (WARN_ON(disconnected && d_is_dir(dentry)))
947 struct dentry *parent = NULL;
949 if (ovl_already_copied_up(dentry, flags))
953 /* find the topmost dentry not yet copied up */
954 for (; !disconnected;) {
955 parent = dget_parent(next);
957 if (ovl_dentry_upper(parent))
964 err = ovl_copy_up_one(parent, next, flags);
969 revert_creds(old_cred);
974 static bool ovl_open_need_copy_up(struct dentry *dentry, int flags)
976 /* Copy up of disconnected dentry does not set upper alias */
977 if (ovl_already_copied_up(dentry, flags))
980 if (special_file(d_inode(dentry)->i_mode))
983 if (!ovl_open_flags_need_copy_up(flags))
989 int ovl_maybe_copy_up(struct dentry *dentry, int flags)
993 if (ovl_open_need_copy_up(dentry, flags)) {
994 err = ovl_want_write(dentry);
996 err = ovl_copy_up_flags(dentry, flags);
997 ovl_drop_write(dentry);
1004 int ovl_copy_up_with_data(struct dentry *dentry)
1006 return ovl_copy_up_flags(dentry, O_WRONLY);
1009 int ovl_copy_up(struct dentry *dentry)
1011 return ovl_copy_up_flags(dentry, 0);