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 path *old, struct path *new, loff_t len)
133 struct file *old_file;
134 struct file *new_file;
138 loff_t data_pos = -1;
140 bool skip_hole = false;
146 old_file = ovl_path_open(old, O_LARGEFILE | O_RDONLY);
147 if (IS_ERR(old_file))
148 return PTR_ERR(old_file);
150 new_file = ovl_path_open(new, O_LARGEFILE | O_WRONLY);
151 if (IS_ERR(new_file)) {
152 error = PTR_ERR(new_file);
156 /* Try to use clone_file_range to clone up within the same fs */
157 cloned = do_clone_file_range(old_file, 0, new_file, 0, len, 0);
160 /* Couldn't clone, so now we try to copy the data */
162 /* Check if lower fs supports seek operation */
163 if (old_file->f_mode & FMODE_LSEEK &&
164 old_file->f_op->llseek)
168 size_t this_len = OVL_COPY_UP_CHUNK_SIZE;
174 if (signal_pending_state(TASK_KILLABLE, current)) {
180 * Fill zero for hole will cost unnecessary disk space
181 * and meanwhile slow down the copy-up speed, so we do
182 * an optimization for hole during copy-up, it relies
183 * on SEEK_DATA implementation in lower fs so if lower
184 * fs does not support it, copy-up will behave as before.
186 * Detail logic of hole detection as below:
187 * When we detect next data position is larger than current
188 * position we will skip that hole, otherwise we copy
189 * data in the size of OVL_COPY_UP_CHUNK_SIZE. Actually,
190 * it may not recognize all kind of holes and sometimes
191 * only skips partial of hole area. However, it will be
192 * enough for most of the use cases.
195 if (skip_hole && data_pos < old_pos) {
196 data_pos = vfs_llseek(old_file, old_pos, SEEK_DATA);
197 if (data_pos > old_pos) {
198 hole_len = data_pos - old_pos;
200 old_pos = new_pos = data_pos;
202 } else if (data_pos == -ENXIO) {
204 } else if (data_pos < 0) {
209 bytes = do_splice_direct(old_file, &old_pos,
211 this_len, SPLICE_F_MOVE);
216 WARN_ON(old_pos != new_pos);
222 error = vfs_fsync(new_file, 0);
229 static int ovl_set_size(struct dentry *upperdentry, struct kstat *stat)
231 struct iattr attr = {
232 .ia_valid = ATTR_SIZE,
233 .ia_size = stat->size,
236 return notify_change(upperdentry, &attr, NULL);
239 static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat)
241 struct iattr attr = {
243 ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET,
244 .ia_atime = stat->atime,
245 .ia_mtime = stat->mtime,
248 return notify_change(upperdentry, &attr, NULL);
251 int ovl_set_attr(struct dentry *upperdentry, struct kstat *stat)
255 if (!S_ISLNK(stat->mode)) {
256 struct iattr attr = {
257 .ia_valid = ATTR_MODE,
258 .ia_mode = stat->mode,
260 err = notify_change(upperdentry, &attr, NULL);
263 struct iattr attr = {
264 .ia_valid = ATTR_UID | ATTR_GID,
268 err = notify_change(upperdentry, &attr, NULL);
271 ovl_set_timestamps(upperdentry, stat);
276 struct ovl_fh *ovl_encode_real_fh(struct dentry *real, bool is_upper)
280 int buflen = MAX_HANDLE_SZ;
281 uuid_t *uuid = &real->d_sb->s_uuid;
284 /* Make sure the real fid stays 32bit aligned */
285 BUILD_BUG_ON(OVL_FH_FID_OFFSET % 4);
286 BUILD_BUG_ON(MAX_HANDLE_SZ + OVL_FH_FID_OFFSET > 255);
288 fh = kzalloc(buflen + OVL_FH_FID_OFFSET, GFP_KERNEL);
290 return ERR_PTR(-ENOMEM);
293 * We encode a non-connectable file handle for non-dir, because we
294 * only need to find the lower inode number and we don't want to pay
295 * the price or reconnecting the dentry.
297 dwords = buflen >> 2;
298 fh_type = exportfs_encode_fh(real, (void *)fh->fb.fid, &dwords, 0);
299 buflen = (dwords << 2);
302 if (WARN_ON(fh_type < 0) ||
303 WARN_ON(buflen > MAX_HANDLE_SZ) ||
304 WARN_ON(fh_type == FILEID_INVALID))
307 fh->fb.version = OVL_FH_VERSION;
308 fh->fb.magic = OVL_FH_MAGIC;
309 fh->fb.type = fh_type;
310 fh->fb.flags = OVL_FH_FLAG_CPU_ENDIAN;
312 * When we will want to decode an overlay dentry from this handle
313 * and all layers are on the same fs, if we get a disconncted real
314 * dentry when we decode fid, the only way to tell if we should assign
315 * it to upperdentry or to lowerstack is by checking this flag.
318 fh->fb.flags |= OVL_FH_FLAG_PATH_UPPER;
319 fh->fb.len = sizeof(fh->fb) + buflen;
329 int ovl_set_origin(struct dentry *dentry, struct dentry *lower,
330 struct dentry *upper)
332 const struct ovl_fh *fh = NULL;
336 * When lower layer doesn't support export operations store a 'null' fh,
337 * so we can use the overlay.origin xattr to distignuish between a copy
338 * up and a pure upper inode.
340 if (ovl_can_decode_fh(lower->d_sb)) {
341 fh = ovl_encode_real_fh(lower, false);
347 * Do not fail when upper doesn't support xattrs.
349 err = ovl_check_setxattr(dentry, upper, OVL_XATTR_ORIGIN, fh->buf,
350 fh ? fh->fb.len : 0, 0);
356 /* Store file handle of @upper dir in @index dir entry */
357 static int ovl_set_upper_fh(struct dentry *upper, struct dentry *index)
359 const struct ovl_fh *fh;
362 fh = ovl_encode_real_fh(upper, true);
366 err = ovl_do_setxattr(index, OVL_XATTR_UPPER, fh->buf, fh->fb.len, 0);
373 * Create and install index entry.
375 * Caller must hold i_mutex on indexdir.
377 static int ovl_create_index(struct dentry *dentry, struct dentry *origin,
378 struct dentry *upper)
380 struct dentry *indexdir = ovl_indexdir(dentry->d_sb);
381 struct inode *dir = d_inode(indexdir);
382 struct dentry *index = NULL;
383 struct dentry *temp = NULL;
384 struct qstr name = { };
388 * For now this is only used for creating index entry for directories,
389 * because non-dir are copied up directly to index and then hardlinked
392 * TODO: implement create index for non-dir, so we can call it when
393 * encoding file handle for non-dir in case index does not exist.
395 if (WARN_ON(!d_is_dir(dentry)))
398 /* Directory not expected to be indexed before copy up */
399 if (WARN_ON(ovl_test_flag(OVL_INDEX, d_inode(dentry))))
402 err = ovl_get_index_name(origin, &name);
406 temp = ovl_create_temp(indexdir, OVL_CATTR(S_IFDIR | 0));
411 err = ovl_set_upper_fh(upper, temp);
415 index = lookup_one_len(name.name, indexdir, name.len);
417 err = PTR_ERR(index);
419 err = ovl_do_rename(dir, temp, dir, index, 0);
424 ovl_cleanup(dir, temp);
431 struct ovl_copy_up_ctx {
432 struct dentry *parent;
433 struct dentry *dentry;
434 struct path lowerpath;
438 struct dentry *destdir;
439 struct qstr destname;
440 struct dentry *workdir;
446 static int ovl_link_up(struct ovl_copy_up_ctx *c)
449 struct dentry *upper;
450 struct dentry *upperdir = ovl_dentry_upper(c->parent);
451 struct inode *udir = d_inode(upperdir);
453 /* Mark parent "impure" because it may now contain non-pure upper */
454 err = ovl_set_impure(c->parent, upperdir);
458 err = ovl_set_nlink_lower(c->dentry);
462 inode_lock_nested(udir, I_MUTEX_PARENT);
463 upper = lookup_one_len(c->dentry->d_name.name, upperdir,
464 c->dentry->d_name.len);
465 err = PTR_ERR(upper);
466 if (!IS_ERR(upper)) {
467 err = ovl_do_link(ovl_dentry_upper(c->dentry), udir, upper);
471 /* Restore timestamps on parent (best effort) */
472 ovl_set_timestamps(upperdir, &c->pstat);
473 ovl_dentry_set_upper_alias(c->dentry);
480 err = ovl_set_nlink_upper(c->dentry);
485 static int ovl_copy_up_inode(struct ovl_copy_up_ctx *c, struct dentry *temp)
490 * Copy up data first and then xattrs. Writing data after
491 * xattrs will remove security.capability xattr automatically.
493 if (S_ISREG(c->stat.mode) && !c->metacopy) {
494 struct path upperpath, datapath;
496 ovl_path_upper(c->dentry, &upperpath);
497 if (WARN_ON(upperpath.dentry != NULL))
499 upperpath.dentry = temp;
501 ovl_path_lowerdata(c->dentry, &datapath);
502 err = ovl_copy_up_data(&datapath, &upperpath, c->stat.size);
507 err = ovl_copy_xattr(c->lowerpath.dentry, temp);
512 * Store identifier of lower inode in upper inode xattr to
513 * allow lookup of the copy up origin inode.
515 * Don't set origin when we are breaking the association with a lower
519 err = ovl_set_origin(c->dentry, c->lowerpath.dentry, temp);
525 err = ovl_check_setxattr(c->dentry, temp, OVL_XATTR_METACOPY,
526 NULL, 0, -EOPNOTSUPP);
531 inode_lock(temp->d_inode);
532 if (S_ISREG(c->stat.mode))
533 err = ovl_set_size(temp, &c->stat);
535 err = ovl_set_attr(temp, &c->stat);
536 inode_unlock(temp->d_inode);
541 struct ovl_cu_creds {
542 const struct cred *old;
546 static int ovl_prep_cu_creds(struct dentry *dentry, struct ovl_cu_creds *cc)
550 cc->old = cc->new = NULL;
551 err = security_inode_copy_up(dentry, &cc->new);
556 cc->old = override_creds(cc->new);
561 static void ovl_revert_cu_creds(struct ovl_cu_creds *cc)
564 revert_creds(cc->old);
570 * Copyup using workdir to prepare temp file. Used when copying up directories,
571 * special files or when upper fs doesn't support O_TMPFILE.
573 static int ovl_copy_up_workdir(struct ovl_copy_up_ctx *c)
576 struct inode *udir = d_inode(c->destdir), *wdir = d_inode(c->workdir);
577 struct dentry *temp, *upper;
578 struct ovl_cu_creds cc;
580 struct ovl_cattr cattr = {
581 /* Can't properly set mode on creation because of the umask */
582 .mode = c->stat.mode & S_IFMT,
583 .rdev = c->stat.rdev,
587 /* workdir and destdir could be the same when copying up to indexdir */
589 if (lock_rename(c->workdir, c->destdir) != NULL)
592 err = ovl_prep_cu_creds(c->dentry, &cc);
596 temp = ovl_create_temp(c->workdir, &cattr);
597 ovl_revert_cu_creds(&cc);
603 err = ovl_copy_up_inode(c, temp);
607 if (S_ISDIR(c->stat.mode) && c->indexed) {
608 err = ovl_create_index(c->dentry, c->lowerpath.dentry, temp);
613 upper = lookup_one_len(c->destname.name, c->destdir, c->destname.len);
614 err = PTR_ERR(upper);
618 err = ovl_do_rename(wdir, temp, udir, upper, 0);
624 ovl_set_upperdata(d_inode(c->dentry));
625 inode = d_inode(c->dentry);
626 ovl_inode_update(inode, temp);
627 if (S_ISDIR(inode->i_mode))
628 ovl_set_flag(OVL_WHITEOUTS, inode);
630 unlock_rename(c->workdir, c->destdir);
635 ovl_cleanup(wdir, temp);
640 /* Copyup using O_TMPFILE which does not require cross dir locking */
641 static int ovl_copy_up_tmpfile(struct ovl_copy_up_ctx *c)
643 struct inode *udir = d_inode(c->destdir);
644 struct dentry *temp, *upper;
645 struct ovl_cu_creds cc;
648 err = ovl_prep_cu_creds(c->dentry, &cc);
652 temp = ovl_do_tmpfile(c->workdir, c->stat.mode);
653 ovl_revert_cu_creds(&cc);
656 return PTR_ERR(temp);
658 err = ovl_copy_up_inode(c, temp);
662 inode_lock_nested(udir, I_MUTEX_PARENT);
664 upper = lookup_one_len(c->destname.name, c->destdir, c->destname.len);
665 err = PTR_ERR(upper);
666 if (!IS_ERR(upper)) {
667 err = ovl_do_link(temp, udir, upper);
676 ovl_set_upperdata(d_inode(c->dentry));
677 ovl_inode_update(d_inode(c->dentry), temp);
687 * Copy up a single dentry
689 * All renames start with copy up of source if necessary. The actual
690 * rename will only proceed once the copy up was successful. Copy up uses
691 * upper parent i_mutex for exclusion. Since rename can change d_parent it
692 * is possible that the copy up will lock the old parent. At that point
693 * the file will have already been copied up anyway.
695 static int ovl_do_copy_up(struct ovl_copy_up_ctx *c)
698 struct ovl_fs *ofs = c->dentry->d_sb->s_fs_info;
699 bool to_index = false;
702 * Indexed non-dir is copied up directly to the index entry and then
703 * hardlinked to upper dir. Indexed dir is copied up to indexdir,
704 * then index entry is created and then copied up dir installed.
705 * Copying dir up to indexdir instead of workdir simplifies locking.
707 if (ovl_need_index(c->dentry)) {
709 if (S_ISDIR(c->stat.mode))
710 c->workdir = ovl_indexdir(c->dentry->d_sb);
715 if (S_ISDIR(c->stat.mode) || c->stat.nlink == 1 || to_index)
719 c->destdir = ovl_indexdir(c->dentry->d_sb);
720 err = ovl_get_index_name(c->lowerpath.dentry, &c->destname);
723 } else if (WARN_ON(!c->parent)) {
724 /* Disconnected dentry must be copied up to index dir */
728 * Mark parent "impure" because it may now contain non-pure
731 err = ovl_set_impure(c->parent, c->destdir);
736 /* Should we copyup with O_TMPFILE or with workdir? */
737 if (S_ISREG(c->stat.mode) && ofs->tmpfile)
738 err = ovl_copy_up_tmpfile(c);
740 err = ovl_copy_up_workdir(c);
745 ovl_set_flag(OVL_INDEX, d_inode(c->dentry));
748 /* Initialize nlink for copy up of disconnected dentry */
749 err = ovl_set_nlink_upper(c->dentry);
751 struct inode *udir = d_inode(c->destdir);
753 /* Restore timestamps on parent (best effort) */
755 ovl_set_timestamps(c->destdir, &c->pstat);
758 ovl_dentry_set_upper_alias(c->dentry);
763 kfree(c->destname.name);
767 static bool ovl_need_meta_copy_up(struct dentry *dentry, umode_t mode,
770 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
772 if (!ofs->config.metacopy)
778 if (flags && ((OPEN_FMODE(flags) & FMODE_WRITE) || (flags & O_TRUNC)))
784 /* Copy up data of an inode which was copied up metadata only in the past. */
785 static int ovl_copy_up_meta_inode_data(struct ovl_copy_up_ctx *c)
787 struct path upperpath, datapath;
789 char *capability = NULL;
790 ssize_t uninitialized_var(cap_size);
792 ovl_path_upper(c->dentry, &upperpath);
793 if (WARN_ON(upperpath.dentry == NULL))
796 ovl_path_lowerdata(c->dentry, &datapath);
797 if (WARN_ON(datapath.dentry == NULL))
801 err = cap_size = ovl_getxattr(upperpath.dentry, XATTR_NAME_CAPS,
803 if (err < 0 && err != -ENODATA)
807 err = ovl_copy_up_data(&datapath, &upperpath, c->stat.size);
812 * Writing to upper file will clear security.capability xattr. We
813 * don't want that to happen for normal copy-up operation.
816 err = ovl_do_setxattr(upperpath.dentry, XATTR_NAME_CAPS,
817 capability, cap_size, 0);
823 err = vfs_removexattr(upperpath.dentry, OVL_XATTR_METACOPY);
827 ovl_set_upperdata(d_inode(c->dentry));
834 static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
838 DEFINE_DELAYED_CALL(done);
839 struct path parentpath;
840 struct ovl_copy_up_ctx ctx = {
843 .workdir = ovl_workdir(dentry),
846 if (WARN_ON(!ctx.workdir))
849 ovl_path_lower(dentry, &ctx.lowerpath);
850 err = vfs_getattr(&ctx.lowerpath, &ctx.stat,
851 STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
855 ctx.metacopy = ovl_need_meta_copy_up(dentry, ctx.stat.mode, flags);
858 ovl_path_upper(parent, &parentpath);
859 ctx.destdir = parentpath.dentry;
860 ctx.destname = dentry->d_name;
862 err = vfs_getattr(&parentpath, &ctx.pstat,
863 STATX_ATIME | STATX_MTIME,
864 AT_STATX_SYNC_AS_STAT);
869 /* maybe truncate regular file. this has no effect on dirs */
873 if (S_ISLNK(ctx.stat.mode)) {
874 ctx.link = vfs_get_link(ctx.lowerpath.dentry, &done);
875 if (IS_ERR(ctx.link))
876 return PTR_ERR(ctx.link);
879 err = ovl_copy_up_start(dentry, flags);
880 /* err < 0: interrupted, err > 0: raced with another copy-up */
885 if (!ovl_dentry_upper(dentry))
886 err = ovl_do_copy_up(&ctx);
887 if (!err && parent && !ovl_dentry_has_upper_alias(dentry))
888 err = ovl_link_up(&ctx);
889 if (!err && ovl_dentry_needs_data_copy_up_locked(dentry, flags))
890 err = ovl_copy_up_meta_inode_data(&ctx);
891 ovl_copy_up_end(dentry);
893 do_delayed_call(&done);
898 int ovl_copy_up_flags(struct dentry *dentry, int flags)
901 const struct cred *old_cred = ovl_override_creds(dentry->d_sb);
902 bool disconnected = (dentry->d_flags & DCACHE_DISCONNECTED);
905 * With NFS export, copy up can get called for a disconnected non-dir.
906 * In this case, we will copy up lower inode to index dir without
907 * linking it to upper dir.
909 if (WARN_ON(disconnected && d_is_dir(dentry)))
914 struct dentry *parent = NULL;
916 if (ovl_already_copied_up(dentry, flags))
920 /* find the topmost dentry not yet copied up */
921 for (; !disconnected;) {
922 parent = dget_parent(next);
924 if (ovl_dentry_upper(parent))
931 err = ovl_copy_up_one(parent, next, flags);
936 revert_creds(old_cred);
941 static bool ovl_open_need_copy_up(struct dentry *dentry, int flags)
943 /* Copy up of disconnected dentry does not set upper alias */
944 if (ovl_already_copied_up(dentry, flags))
947 if (special_file(d_inode(dentry)->i_mode))
950 if (!ovl_open_flags_need_copy_up(flags))
956 int ovl_maybe_copy_up(struct dentry *dentry, int flags)
960 if (ovl_open_need_copy_up(dentry, flags)) {
961 err = ovl_want_write(dentry);
963 err = ovl_copy_up_flags(dentry, flags);
964 ovl_drop_write(dentry);
971 int ovl_copy_up_with_data(struct dentry *dentry)
973 return ovl_copy_up_flags(dentry, O_WRONLY);
976 int ovl_copy_up(struct dentry *dentry)
978 return ovl_copy_up_flags(dentry, 0);