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 dentry *old, struct dentry *new)
48 ssize_t list_size, size, value_size = 0;
49 char *buf, *name, *value = NULL;
53 if (!(old->d_inode->i_opflags & IOP_XATTR) ||
54 !(new->d_inode->i_opflags & IOP_XATTR))
57 list_size = vfs_listxattr(old, NULL, 0);
59 if (list_size == -EOPNOTSUPP)
64 buf = kzalloc(list_size, GFP_KERNEL);
68 list_size = vfs_listxattr(old, buf, list_size);
74 for (name = buf; list_size; name += slen) {
75 slen = strnlen(name, list_size) + 1;
77 /* underlying fs providing us with an broken xattr list? */
78 if (WARN_ON(slen > list_size)) {
84 if (ovl_is_private_xattr(name))
87 size = vfs_getxattr(old, name, value, value_size);
89 size = vfs_getxattr(old, name, NULL, 0);
96 if (size > value_size) {
99 new = krealloc(value, size, GFP_KERNEL);
109 error = security_inode_copy_up_xattr(name);
110 if (error < 0 && error != -EOPNOTSUPP)
114 continue; /* Discard */
116 error = vfs_setxattr(new, name, value, size, 0);
118 if (error != -EOPNOTSUPP || ovl_must_copy_xattr(name))
121 /* Ignore failure to copy unknown xattrs */
131 static int ovl_copy_up_data(struct ovl_fs *ofs, struct path *old,
132 struct path *new, loff_t len)
134 struct file *old_file;
135 struct file *new_file;
139 loff_t data_pos = -1;
141 bool skip_hole = false;
147 old_file = ovl_path_open(old, O_LARGEFILE | O_RDONLY);
148 if (IS_ERR(old_file))
149 return PTR_ERR(old_file);
151 new_file = ovl_path_open(new, O_LARGEFILE | O_WRONLY);
152 if (IS_ERR(new_file)) {
153 error = PTR_ERR(new_file);
157 /* Try to use clone_file_range to clone up within the same fs */
158 cloned = do_clone_file_range(old_file, 0, new_file, 0, len, 0);
161 /* Couldn't clone, so now we try to copy the data */
163 /* Check if lower fs supports seek operation */
164 if (old_file->f_mode & FMODE_LSEEK &&
165 old_file->f_op->llseek)
169 size_t this_len = OVL_COPY_UP_CHUNK_SIZE;
175 if (signal_pending_state(TASK_KILLABLE, current)) {
181 * Fill zero for hole will cost unnecessary disk space
182 * and meanwhile slow down the copy-up speed, so we do
183 * an optimization for hole during copy-up, it relies
184 * on SEEK_DATA implementation in lower fs so if lower
185 * fs does not support it, copy-up will behave as before.
187 * Detail logic of hole detection as below:
188 * When we detect next data position is larger than current
189 * position we will skip that hole, otherwise we copy
190 * data in the size of OVL_COPY_UP_CHUNK_SIZE. Actually,
191 * it may not recognize all kind of holes and sometimes
192 * only skips partial of hole area. However, it will be
193 * enough for most of the use cases.
196 if (skip_hole && data_pos < old_pos) {
197 data_pos = vfs_llseek(old_file, old_pos, SEEK_DATA);
198 if (data_pos > old_pos) {
199 hole_len = data_pos - old_pos;
201 old_pos = new_pos = data_pos;
203 } else if (data_pos == -ENXIO) {
205 } else if (data_pos < 0) {
210 bytes = do_splice_direct(old_file, &old_pos,
212 this_len, SPLICE_F_MOVE);
217 WARN_ON(old_pos != new_pos);
222 if (!error && ovl_should_sync(ofs))
223 error = vfs_fsync(new_file, 0);
230 static int ovl_set_size(struct dentry *upperdentry, struct kstat *stat)
232 struct iattr attr = {
233 .ia_valid = ATTR_SIZE,
234 .ia_size = stat->size,
237 return notify_change(upperdentry, &attr, NULL);
240 static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat)
242 struct iattr attr = {
244 ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET,
245 .ia_atime = stat->atime,
246 .ia_mtime = stat->mtime,
249 return notify_change(upperdentry, &attr, NULL);
252 int ovl_set_attr(struct dentry *upperdentry, struct kstat *stat)
256 if (!S_ISLNK(stat->mode)) {
257 struct iattr attr = {
258 .ia_valid = ATTR_MODE,
259 .ia_mode = stat->mode,
261 err = notify_change(upperdentry, &attr, NULL);
264 struct iattr attr = {
265 .ia_valid = ATTR_UID | ATTR_GID,
269 err = notify_change(upperdentry, &attr, NULL);
272 ovl_set_timestamps(upperdentry, stat);
277 struct ovl_fh *ovl_encode_real_fh(struct dentry *real, bool is_upper)
281 int buflen = MAX_HANDLE_SZ;
282 uuid_t *uuid = &real->d_sb->s_uuid;
285 /* Make sure the real fid stays 32bit aligned */
286 BUILD_BUG_ON(OVL_FH_FID_OFFSET % 4);
287 BUILD_BUG_ON(MAX_HANDLE_SZ + OVL_FH_FID_OFFSET > 255);
289 fh = kzalloc(buflen + OVL_FH_FID_OFFSET, GFP_KERNEL);
291 return ERR_PTR(-ENOMEM);
294 * We encode a non-connectable file handle for non-dir, because we
295 * only need to find the lower inode number and we don't want to pay
296 * the price or reconnecting the dentry.
298 dwords = buflen >> 2;
299 fh_type = exportfs_encode_fh(real, (void *)fh->fb.fid, &dwords, 0);
300 buflen = (dwords << 2);
303 if (WARN_ON(fh_type < 0) ||
304 WARN_ON(buflen > MAX_HANDLE_SZ) ||
305 WARN_ON(fh_type == FILEID_INVALID))
308 fh->fb.version = OVL_FH_VERSION;
309 fh->fb.magic = OVL_FH_MAGIC;
310 fh->fb.type = fh_type;
311 fh->fb.flags = OVL_FH_FLAG_CPU_ENDIAN;
313 * When we will want to decode an overlay dentry from this handle
314 * and all layers are on the same fs, if we get a disconncted real
315 * dentry when we decode fid, the only way to tell if we should assign
316 * it to upperdentry or to lowerstack is by checking this flag.
319 fh->fb.flags |= OVL_FH_FLAG_PATH_UPPER;
320 fh->fb.len = sizeof(fh->fb) + buflen;
330 int ovl_set_origin(struct dentry *dentry, struct dentry *lower,
331 struct dentry *upper)
333 const struct ovl_fh *fh = NULL;
337 * When lower layer doesn't support export operations store a 'null' fh,
338 * so we can use the overlay.origin xattr to distignuish between a copy
339 * up and a pure upper inode.
341 if (ovl_can_decode_fh(lower->d_sb)) {
342 fh = ovl_encode_real_fh(lower, false);
348 * Do not fail when upper doesn't support xattrs.
350 err = ovl_check_setxattr(dentry, upper, OVL_XATTR_ORIGIN, fh->buf,
351 fh ? fh->fb.len : 0, 0);
357 /* Store file handle of @upper dir in @index dir entry */
358 static int ovl_set_upper_fh(struct dentry *upper, struct dentry *index)
360 const struct ovl_fh *fh;
363 fh = ovl_encode_real_fh(upper, true);
367 err = ovl_do_setxattr(index, OVL_XATTR_UPPER, fh->buf, fh->fb.len, 0);
374 * Create and install index entry.
376 * Caller must hold i_mutex on indexdir.
378 static int ovl_create_index(struct dentry *dentry, struct dentry *origin,
379 struct dentry *upper)
381 struct dentry *indexdir = ovl_indexdir(dentry->d_sb);
382 struct inode *dir = d_inode(indexdir);
383 struct dentry *index = NULL;
384 struct dentry *temp = NULL;
385 struct qstr name = { };
389 * For now this is only used for creating index entry for directories,
390 * because non-dir are copied up directly to index and then hardlinked
393 * TODO: implement create index for non-dir, so we can call it when
394 * encoding file handle for non-dir in case index does not exist.
396 if (WARN_ON(!d_is_dir(dentry)))
399 /* Directory not expected to be indexed before copy up */
400 if (WARN_ON(ovl_test_flag(OVL_INDEX, d_inode(dentry))))
403 err = ovl_get_index_name(origin, &name);
407 temp = ovl_create_temp(indexdir, OVL_CATTR(S_IFDIR | 0));
412 err = ovl_set_upper_fh(upper, temp);
416 index = lookup_one_len(name.name, indexdir, name.len);
418 err = PTR_ERR(index);
420 err = ovl_do_rename(dir, temp, dir, index, 0);
425 ovl_cleanup(dir, temp);
432 struct ovl_copy_up_ctx {
433 struct dentry *parent;
434 struct dentry *dentry;
435 struct path lowerpath;
439 struct dentry *destdir;
440 struct qstr destname;
441 struct dentry *workdir;
447 static int ovl_link_up(struct ovl_copy_up_ctx *c)
450 struct dentry *upper;
451 struct dentry *upperdir = ovl_dentry_upper(c->parent);
452 struct inode *udir = d_inode(upperdir);
454 /* Mark parent "impure" because it may now contain non-pure upper */
455 err = ovl_set_impure(c->parent, upperdir);
459 err = ovl_set_nlink_lower(c->dentry);
463 inode_lock_nested(udir, I_MUTEX_PARENT);
464 upper = lookup_one_len(c->dentry->d_name.name, upperdir,
465 c->dentry->d_name.len);
466 err = PTR_ERR(upper);
467 if (!IS_ERR(upper)) {
468 err = ovl_do_link(ovl_dentry_upper(c->dentry), udir, upper);
472 /* Restore timestamps on parent (best effort) */
473 ovl_set_timestamps(upperdir, &c->pstat);
474 ovl_dentry_set_upper_alias(c->dentry);
481 err = ovl_set_nlink_upper(c->dentry);
486 static int ovl_copy_up_inode(struct ovl_copy_up_ctx *c, struct dentry *temp)
488 struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb);
492 * Copy up data first and then xattrs. Writing data after
493 * xattrs will remove security.capability xattr automatically.
495 if (S_ISREG(c->stat.mode) && !c->metacopy) {
496 struct path upperpath, datapath;
498 ovl_path_upper(c->dentry, &upperpath);
499 if (WARN_ON(upperpath.dentry != NULL))
501 upperpath.dentry = temp;
503 ovl_path_lowerdata(c->dentry, &datapath);
504 err = ovl_copy_up_data(ofs, &datapath, &upperpath,
510 err = ovl_copy_xattr(c->lowerpath.dentry, temp);
515 * Store identifier of lower inode in upper inode xattr to
516 * allow lookup of the copy up origin inode.
518 * Don't set origin when we are breaking the association with a lower
522 err = ovl_set_origin(c->dentry, c->lowerpath.dentry, temp);
528 err = ovl_check_setxattr(c->dentry, temp, OVL_XATTR_METACOPY,
529 NULL, 0, -EOPNOTSUPP);
534 inode_lock(temp->d_inode);
535 if (S_ISREG(c->stat.mode))
536 err = ovl_set_size(temp, &c->stat);
538 err = ovl_set_attr(temp, &c->stat);
539 inode_unlock(temp->d_inode);
544 struct ovl_cu_creds {
545 const struct cred *old;
549 static int ovl_prep_cu_creds(struct dentry *dentry, struct ovl_cu_creds *cc)
553 cc->old = cc->new = NULL;
554 err = security_inode_copy_up(dentry, &cc->new);
559 cc->old = override_creds(cc->new);
564 static void ovl_revert_cu_creds(struct ovl_cu_creds *cc)
567 revert_creds(cc->old);
573 * Copyup using workdir to prepare temp file. Used when copying up directories,
574 * special files or when upper fs doesn't support O_TMPFILE.
576 static int ovl_copy_up_workdir(struct ovl_copy_up_ctx *c)
579 struct inode *udir = d_inode(c->destdir), *wdir = d_inode(c->workdir);
580 struct dentry *temp, *upper;
581 struct ovl_cu_creds cc;
583 struct ovl_cattr cattr = {
584 /* Can't properly set mode on creation because of the umask */
585 .mode = c->stat.mode & S_IFMT,
586 .rdev = c->stat.rdev,
590 /* workdir and destdir could be the same when copying up to indexdir */
592 if (lock_rename(c->workdir, c->destdir) != NULL)
595 err = ovl_prep_cu_creds(c->dentry, &cc);
599 temp = ovl_create_temp(c->workdir, &cattr);
600 ovl_revert_cu_creds(&cc);
606 err = ovl_copy_up_inode(c, temp);
610 if (S_ISDIR(c->stat.mode) && c->indexed) {
611 err = ovl_create_index(c->dentry, c->lowerpath.dentry, temp);
616 upper = lookup_one_len(c->destname.name, c->destdir, c->destname.len);
617 err = PTR_ERR(upper);
621 err = ovl_do_rename(wdir, temp, udir, upper, 0);
627 ovl_set_upperdata(d_inode(c->dentry));
628 inode = d_inode(c->dentry);
629 ovl_inode_update(inode, temp);
630 if (S_ISDIR(inode->i_mode))
631 ovl_set_flag(OVL_WHITEOUTS, inode);
633 unlock_rename(c->workdir, c->destdir);
638 ovl_cleanup(wdir, temp);
643 /* Copyup using O_TMPFILE which does not require cross dir locking */
644 static int ovl_copy_up_tmpfile(struct ovl_copy_up_ctx *c)
646 struct inode *udir = d_inode(c->destdir);
647 struct dentry *temp, *upper;
648 struct ovl_cu_creds cc;
651 err = ovl_prep_cu_creds(c->dentry, &cc);
655 temp = ovl_do_tmpfile(c->workdir, c->stat.mode);
656 ovl_revert_cu_creds(&cc);
659 return PTR_ERR(temp);
661 err = ovl_copy_up_inode(c, temp);
665 inode_lock_nested(udir, I_MUTEX_PARENT);
667 upper = lookup_one_len(c->destname.name, c->destdir, c->destname.len);
668 err = PTR_ERR(upper);
669 if (!IS_ERR(upper)) {
670 err = ovl_do_link(temp, udir, upper);
679 ovl_set_upperdata(d_inode(c->dentry));
680 ovl_inode_update(d_inode(c->dentry), temp);
690 * Copy up a single dentry
692 * All renames start with copy up of source if necessary. The actual
693 * rename will only proceed once the copy up was successful. Copy up uses
694 * upper parent i_mutex for exclusion. Since rename can change d_parent it
695 * is possible that the copy up will lock the old parent. At that point
696 * the file will have already been copied up anyway.
698 static int ovl_do_copy_up(struct ovl_copy_up_ctx *c)
701 struct ovl_fs *ofs = c->dentry->d_sb->s_fs_info;
702 bool to_index = false;
705 * Indexed non-dir is copied up directly to the index entry and then
706 * hardlinked to upper dir. Indexed dir is copied up to indexdir,
707 * then index entry is created and then copied up dir installed.
708 * Copying dir up to indexdir instead of workdir simplifies locking.
710 if (ovl_need_index(c->dentry)) {
712 if (S_ISDIR(c->stat.mode))
713 c->workdir = ovl_indexdir(c->dentry->d_sb);
718 if (S_ISDIR(c->stat.mode) || c->stat.nlink == 1 || to_index)
722 c->destdir = ovl_indexdir(c->dentry->d_sb);
723 err = ovl_get_index_name(c->lowerpath.dentry, &c->destname);
726 } else if (WARN_ON(!c->parent)) {
727 /* Disconnected dentry must be copied up to index dir */
731 * Mark parent "impure" because it may now contain non-pure
734 err = ovl_set_impure(c->parent, c->destdir);
739 /* Should we copyup with O_TMPFILE or with workdir? */
740 if (S_ISREG(c->stat.mode) && ofs->tmpfile)
741 err = ovl_copy_up_tmpfile(c);
743 err = ovl_copy_up_workdir(c);
748 ovl_set_flag(OVL_INDEX, d_inode(c->dentry));
751 /* Initialize nlink for copy up of disconnected dentry */
752 err = ovl_set_nlink_upper(c->dentry);
754 struct inode *udir = d_inode(c->destdir);
756 /* Restore timestamps on parent (best effort) */
758 ovl_set_timestamps(c->destdir, &c->pstat);
761 ovl_dentry_set_upper_alias(c->dentry);
766 kfree(c->destname.name);
770 static bool ovl_need_meta_copy_up(struct dentry *dentry, umode_t mode,
773 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
775 if (!ofs->config.metacopy)
781 if (flags && ((OPEN_FMODE(flags) & FMODE_WRITE) || (flags & O_TRUNC)))
787 static ssize_t ovl_getxattr(struct dentry *dentry, char *name, char **value)
792 res = vfs_getxattr(dentry, name, NULL, 0);
793 if (res == -ENODATA || res == -EOPNOTSUPP)
797 buf = kzalloc(res, GFP_KERNEL);
801 res = vfs_getxattr(dentry, name, buf, res);
810 /* Copy up data of an inode which was copied up metadata only in the past. */
811 static int ovl_copy_up_meta_inode_data(struct ovl_copy_up_ctx *c)
813 struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb);
814 struct path upperpath, datapath;
816 char *capability = NULL;
819 ovl_path_upper(c->dentry, &upperpath);
820 if (WARN_ON(upperpath.dentry == NULL))
823 ovl_path_lowerdata(c->dentry, &datapath);
824 if (WARN_ON(datapath.dentry == NULL))
828 err = cap_size = ovl_getxattr(upperpath.dentry, XATTR_NAME_CAPS,
834 err = ovl_copy_up_data(ofs, &datapath, &upperpath, c->stat.size);
839 * Writing to upper file will clear security.capability xattr. We
840 * don't want that to happen for normal copy-up operation.
843 err = vfs_setxattr(upperpath.dentry, XATTR_NAME_CAPS,
844 capability, cap_size, 0);
850 err = ovl_do_removexattr(upperpath.dentry, OVL_XATTR_METACOPY);
854 ovl_set_upperdata(d_inode(c->dentry));
861 static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
865 DEFINE_DELAYED_CALL(done);
866 struct path parentpath;
867 struct ovl_copy_up_ctx ctx = {
870 .workdir = ovl_workdir(dentry),
873 if (WARN_ON(!ctx.workdir))
876 ovl_path_lower(dentry, &ctx.lowerpath);
877 err = vfs_getattr(&ctx.lowerpath, &ctx.stat,
878 STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
882 ctx.metacopy = ovl_need_meta_copy_up(dentry, ctx.stat.mode, flags);
885 ovl_path_upper(parent, &parentpath);
886 ctx.destdir = parentpath.dentry;
887 ctx.destname = dentry->d_name;
889 err = vfs_getattr(&parentpath, &ctx.pstat,
890 STATX_ATIME | STATX_MTIME,
891 AT_STATX_SYNC_AS_STAT);
896 /* maybe truncate regular file. this has no effect on dirs */
900 if (S_ISLNK(ctx.stat.mode)) {
901 ctx.link = vfs_get_link(ctx.lowerpath.dentry, &done);
902 if (IS_ERR(ctx.link))
903 return PTR_ERR(ctx.link);
906 err = ovl_copy_up_start(dentry, flags);
907 /* err < 0: interrupted, err > 0: raced with another copy-up */
912 if (!ovl_dentry_upper(dentry))
913 err = ovl_do_copy_up(&ctx);
914 if (!err && parent && !ovl_dentry_has_upper_alias(dentry))
915 err = ovl_link_up(&ctx);
916 if (!err && ovl_dentry_needs_data_copy_up_locked(dentry, flags))
917 err = ovl_copy_up_meta_inode_data(&ctx);
918 ovl_copy_up_end(dentry);
920 do_delayed_call(&done);
925 static int ovl_copy_up_flags(struct dentry *dentry, int flags)
928 const struct cred *old_cred = ovl_override_creds(dentry->d_sb);
929 bool disconnected = (dentry->d_flags & DCACHE_DISCONNECTED);
932 * With NFS export, copy up can get called for a disconnected non-dir.
933 * In this case, we will copy up lower inode to index dir without
934 * linking it to upper dir.
936 if (WARN_ON(disconnected && d_is_dir(dentry)))
941 struct dentry *parent = NULL;
943 if (ovl_already_copied_up(dentry, flags))
947 /* find the topmost dentry not yet copied up */
948 for (; !disconnected;) {
949 parent = dget_parent(next);
951 if (ovl_dentry_upper(parent))
958 err = ovl_copy_up_one(parent, next, flags);
963 revert_creds(old_cred);
968 static bool ovl_open_need_copy_up(struct dentry *dentry, int flags)
970 /* Copy up of disconnected dentry does not set upper alias */
971 if (ovl_already_copied_up(dentry, flags))
974 if (special_file(d_inode(dentry)->i_mode))
977 if (!ovl_open_flags_need_copy_up(flags))
983 int ovl_maybe_copy_up(struct dentry *dentry, int flags)
987 if (ovl_open_need_copy_up(dentry, flags)) {
988 err = ovl_want_write(dentry);
990 err = ovl_copy_up_flags(dentry, flags);
991 ovl_drop_write(dentry);
998 int ovl_copy_up_with_data(struct dentry *dentry)
1000 return ovl_copy_up_flags(dentry, O_WRONLY);
1003 int ovl_copy_up(struct dentry *dentry)
1005 return ovl_copy_up_flags(dentry, 0);