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 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;
43 int uninitialized_var(error);
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 size = vfs_getxattr(old, name, value, value_size);
82 size = vfs_getxattr(old, name, NULL, 0);
89 if (size > value_size) {
92 new = krealloc(value, size, GFP_KERNEL);
102 error = security_inode_copy_up_xattr(name);
103 if (error < 0 && error != -EOPNOTSUPP)
107 continue; /* Discard */
109 error = vfs_setxattr(new, name, value, size, 0);
119 static int ovl_copy_up_data(struct path *old, struct path *new, loff_t len)
121 struct file *old_file;
122 struct file *new_file;
126 loff_t data_pos = -1;
128 bool skip_hole = false;
134 old_file = ovl_path_open(old, O_LARGEFILE | O_RDONLY);
135 if (IS_ERR(old_file))
136 return PTR_ERR(old_file);
138 new_file = ovl_path_open(new, O_LARGEFILE | O_WRONLY);
139 if (IS_ERR(new_file)) {
140 error = PTR_ERR(new_file);
144 /* Try to use clone_file_range to clone up within the same fs */
145 cloned = do_clone_file_range(old_file, 0, new_file, 0, len, 0);
148 /* Couldn't clone, so now we try to copy the data */
150 /* Check if lower fs supports seek operation */
151 if (old_file->f_mode & FMODE_LSEEK &&
152 old_file->f_op->llseek)
156 size_t this_len = OVL_COPY_UP_CHUNK_SIZE;
162 if (signal_pending_state(TASK_KILLABLE, current)) {
168 * Fill zero for hole will cost unnecessary disk space
169 * and meanwhile slow down the copy-up speed, so we do
170 * an optimization for hole during copy-up, it relies
171 * on SEEK_DATA implementation in lower fs so if lower
172 * fs does not support it, copy-up will behave as before.
174 * Detail logic of hole detection as below:
175 * When we detect next data position is larger than current
176 * position we will skip that hole, otherwise we copy
177 * data in the size of OVL_COPY_UP_CHUNK_SIZE. Actually,
178 * it may not recognize all kind of holes and sometimes
179 * only skips partial of hole area. However, it will be
180 * enough for most of the use cases.
183 if (skip_hole && data_pos < old_pos) {
184 data_pos = vfs_llseek(old_file, old_pos, SEEK_DATA);
185 if (data_pos > old_pos) {
186 hole_len = data_pos - old_pos;
188 old_pos = new_pos = data_pos;
190 } else if (data_pos == -ENXIO) {
192 } else if (data_pos < 0) {
197 bytes = do_splice_direct(old_file, &old_pos,
199 this_len, SPLICE_F_MOVE);
204 WARN_ON(old_pos != new_pos);
210 error = vfs_fsync(new_file, 0);
217 static int ovl_set_size(struct dentry *upperdentry, struct kstat *stat)
219 struct iattr attr = {
220 .ia_valid = ATTR_SIZE,
221 .ia_size = stat->size,
224 return notify_change(upperdentry, &attr, NULL);
227 static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat)
229 struct iattr attr = {
231 ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET,
232 .ia_atime = stat->atime,
233 .ia_mtime = stat->mtime,
236 return notify_change(upperdentry, &attr, NULL);
239 int ovl_set_attr(struct dentry *upperdentry, struct kstat *stat)
243 if (!S_ISLNK(stat->mode)) {
244 struct iattr attr = {
245 .ia_valid = ATTR_MODE,
246 .ia_mode = stat->mode,
248 err = notify_change(upperdentry, &attr, NULL);
251 struct iattr attr = {
252 .ia_valid = ATTR_UID | ATTR_GID,
256 err = notify_change(upperdentry, &attr, NULL);
259 ovl_set_timestamps(upperdentry, stat);
264 struct ovl_fh *ovl_encode_real_fh(struct dentry *real, bool is_upper)
268 int buflen = MAX_HANDLE_SZ;
269 uuid_t *uuid = &real->d_sb->s_uuid;
272 /* Make sure the real fid stays 32bit aligned */
273 BUILD_BUG_ON(OVL_FH_FID_OFFSET % 4);
274 BUILD_BUG_ON(MAX_HANDLE_SZ + OVL_FH_FID_OFFSET > 255);
276 fh = kzalloc(buflen + OVL_FH_FID_OFFSET, GFP_KERNEL);
278 return ERR_PTR(-ENOMEM);
281 * We encode a non-connectable file handle for non-dir, because we
282 * only need to find the lower inode number and we don't want to pay
283 * the price or reconnecting the dentry.
285 dwords = buflen >> 2;
286 fh_type = exportfs_encode_fh(real, (void *)fh->fb.fid, &dwords, 0);
287 buflen = (dwords << 2);
290 if (WARN_ON(fh_type < 0) ||
291 WARN_ON(buflen > MAX_HANDLE_SZ) ||
292 WARN_ON(fh_type == FILEID_INVALID))
295 fh->fb.version = OVL_FH_VERSION;
296 fh->fb.magic = OVL_FH_MAGIC;
297 fh->fb.type = fh_type;
298 fh->fb.flags = OVL_FH_FLAG_CPU_ENDIAN;
300 * When we will want to decode an overlay dentry from this handle
301 * and all layers are on the same fs, if we get a disconncted real
302 * dentry when we decode fid, the only way to tell if we should assign
303 * it to upperdentry or to lowerstack is by checking this flag.
306 fh->fb.flags |= OVL_FH_FLAG_PATH_UPPER;
307 fh->fb.len = sizeof(fh->fb) + buflen;
317 int ovl_set_origin(struct dentry *dentry, struct dentry *lower,
318 struct dentry *upper)
320 const struct ovl_fh *fh = NULL;
324 * When lower layer doesn't support export operations store a 'null' fh,
325 * so we can use the overlay.origin xattr to distignuish between a copy
326 * up and a pure upper inode.
328 if (ovl_can_decode_fh(lower->d_sb)) {
329 fh = ovl_encode_real_fh(lower, false);
335 * Do not fail when upper doesn't support xattrs.
337 err = ovl_check_setxattr(dentry, upper, OVL_XATTR_ORIGIN, fh->buf,
338 fh ? fh->fb.len : 0, 0);
344 /* Store file handle of @upper dir in @index dir entry */
345 static int ovl_set_upper_fh(struct dentry *upper, struct dentry *index)
347 const struct ovl_fh *fh;
350 fh = ovl_encode_real_fh(upper, true);
354 err = ovl_do_setxattr(index, OVL_XATTR_UPPER, fh->buf, fh->fb.len, 0);
361 * Create and install index entry.
363 * Caller must hold i_mutex on indexdir.
365 static int ovl_create_index(struct dentry *dentry, struct dentry *origin,
366 struct dentry *upper)
368 struct dentry *indexdir = ovl_indexdir(dentry->d_sb);
369 struct inode *dir = d_inode(indexdir);
370 struct dentry *index = NULL;
371 struct dentry *temp = NULL;
372 struct qstr name = { };
376 * For now this is only used for creating index entry for directories,
377 * because non-dir are copied up directly to index and then hardlinked
380 * TODO: implement create index for non-dir, so we can call it when
381 * encoding file handle for non-dir in case index does not exist.
383 if (WARN_ON(!d_is_dir(dentry)))
386 /* Directory not expected to be indexed before copy up */
387 if (WARN_ON(ovl_test_flag(OVL_INDEX, d_inode(dentry))))
390 err = ovl_get_index_name(origin, &name);
394 temp = ovl_create_temp(indexdir, OVL_CATTR(S_IFDIR | 0));
399 err = ovl_set_upper_fh(upper, temp);
403 index = lookup_one_len(name.name, indexdir, name.len);
405 err = PTR_ERR(index);
407 err = ovl_do_rename(dir, temp, dir, index, 0);
412 ovl_cleanup(dir, temp);
419 struct ovl_copy_up_ctx {
420 struct dentry *parent;
421 struct dentry *dentry;
422 struct path lowerpath;
426 struct dentry *destdir;
427 struct qstr destname;
428 struct dentry *workdir;
434 static int ovl_link_up(struct ovl_copy_up_ctx *c)
437 struct dentry *upper;
438 struct dentry *upperdir = ovl_dentry_upper(c->parent);
439 struct inode *udir = d_inode(upperdir);
441 /* Mark parent "impure" because it may now contain non-pure upper */
442 err = ovl_set_impure(c->parent, upperdir);
446 err = ovl_set_nlink_lower(c->dentry);
450 inode_lock_nested(udir, I_MUTEX_PARENT);
451 upper = lookup_one_len(c->dentry->d_name.name, upperdir,
452 c->dentry->d_name.len);
453 err = PTR_ERR(upper);
454 if (!IS_ERR(upper)) {
455 err = ovl_do_link(ovl_dentry_upper(c->dentry), udir, upper);
459 /* Restore timestamps on parent (best effort) */
460 ovl_set_timestamps(upperdir, &c->pstat);
461 ovl_dentry_set_upper_alias(c->dentry);
468 err = ovl_set_nlink_upper(c->dentry);
473 static int ovl_copy_up_inode(struct ovl_copy_up_ctx *c, struct dentry *temp)
478 * Copy up data first and then xattrs. Writing data after
479 * xattrs will remove security.capability xattr automatically.
481 if (S_ISREG(c->stat.mode) && !c->metacopy) {
482 struct path upperpath, datapath;
484 ovl_path_upper(c->dentry, &upperpath);
485 if (WARN_ON(upperpath.dentry != NULL))
487 upperpath.dentry = temp;
489 ovl_path_lowerdata(c->dentry, &datapath);
490 err = ovl_copy_up_data(&datapath, &upperpath, c->stat.size);
495 err = ovl_copy_xattr(c->lowerpath.dentry, temp);
500 * Store identifier of lower inode in upper inode xattr to
501 * allow lookup of the copy up origin inode.
503 * Don't set origin when we are breaking the association with a lower
507 err = ovl_set_origin(c->dentry, c->lowerpath.dentry, temp);
513 err = ovl_check_setxattr(c->dentry, temp, OVL_XATTR_METACOPY,
514 NULL, 0, -EOPNOTSUPP);
519 inode_lock(temp->d_inode);
520 if (S_ISREG(c->stat.mode))
521 err = ovl_set_size(temp, &c->stat);
523 err = ovl_set_attr(temp, &c->stat);
524 inode_unlock(temp->d_inode);
529 struct ovl_cu_creds {
530 const struct cred *old;
534 static int ovl_prep_cu_creds(struct dentry *dentry, struct ovl_cu_creds *cc)
538 cc->old = cc->new = NULL;
539 err = security_inode_copy_up(dentry, &cc->new);
544 cc->old = override_creds(cc->new);
549 static void ovl_revert_cu_creds(struct ovl_cu_creds *cc)
552 revert_creds(cc->old);
558 * Copyup using workdir to prepare temp file. Used when copying up directories,
559 * special files or when upper fs doesn't support O_TMPFILE.
561 static int ovl_copy_up_workdir(struct ovl_copy_up_ctx *c)
564 struct inode *udir = d_inode(c->destdir), *wdir = d_inode(c->workdir);
565 struct dentry *temp, *upper;
566 struct ovl_cu_creds cc;
568 struct ovl_cattr cattr = {
569 /* Can't properly set mode on creation because of the umask */
570 .mode = c->stat.mode & S_IFMT,
571 .rdev = c->stat.rdev,
575 err = ovl_lock_rename_workdir(c->workdir, c->destdir);
579 err = ovl_prep_cu_creds(c->dentry, &cc);
583 temp = ovl_create_temp(c->workdir, &cattr);
584 ovl_revert_cu_creds(&cc);
590 err = ovl_copy_up_inode(c, temp);
594 if (S_ISDIR(c->stat.mode) && c->indexed) {
595 err = ovl_create_index(c->dentry, c->lowerpath.dentry, temp);
600 upper = lookup_one_len(c->destname.name, c->destdir, c->destname.len);
601 err = PTR_ERR(upper);
605 err = ovl_do_rename(wdir, temp, udir, upper, 0);
611 ovl_set_upperdata(d_inode(c->dentry));
612 inode = d_inode(c->dentry);
613 ovl_inode_update(inode, temp);
614 if (S_ISDIR(inode->i_mode))
615 ovl_set_flag(OVL_WHITEOUTS, inode);
617 unlock_rename(c->workdir, c->destdir);
622 ovl_cleanup(wdir, temp);
627 /* Copyup using O_TMPFILE which does not require cross dir locking */
628 static int ovl_copy_up_tmpfile(struct ovl_copy_up_ctx *c)
630 struct inode *udir = d_inode(c->destdir);
631 struct dentry *temp, *upper;
632 struct ovl_cu_creds cc;
635 err = ovl_prep_cu_creds(c->dentry, &cc);
639 temp = ovl_do_tmpfile(c->workdir, c->stat.mode);
640 ovl_revert_cu_creds(&cc);
643 return PTR_ERR(temp);
645 err = ovl_copy_up_inode(c, temp);
649 inode_lock_nested(udir, I_MUTEX_PARENT);
651 upper = lookup_one_len(c->destname.name, c->destdir, c->destname.len);
652 err = PTR_ERR(upper);
653 if (!IS_ERR(upper)) {
654 err = ovl_do_link(temp, udir, upper);
663 ovl_set_upperdata(d_inode(c->dentry));
664 ovl_inode_update(d_inode(c->dentry), temp);
674 * Copy up a single dentry
676 * All renames start with copy up of source if necessary. The actual
677 * rename will only proceed once the copy up was successful. Copy up uses
678 * upper parent i_mutex for exclusion. Since rename can change d_parent it
679 * is possible that the copy up will lock the old parent. At that point
680 * the file will have already been copied up anyway.
682 static int ovl_do_copy_up(struct ovl_copy_up_ctx *c)
685 struct ovl_fs *ofs = c->dentry->d_sb->s_fs_info;
686 bool to_index = false;
689 * Indexed non-dir is copied up directly to the index entry and then
690 * hardlinked to upper dir. Indexed dir is copied up to indexdir,
691 * then index entry is created and then copied up dir installed.
692 * Copying dir up to indexdir instead of workdir simplifies locking.
694 if (ovl_need_index(c->dentry)) {
696 if (S_ISDIR(c->stat.mode))
697 c->workdir = ovl_indexdir(c->dentry->d_sb);
702 if (S_ISDIR(c->stat.mode) || c->stat.nlink == 1 || to_index)
706 c->destdir = ovl_indexdir(c->dentry->d_sb);
707 err = ovl_get_index_name(c->lowerpath.dentry, &c->destname);
710 } else if (WARN_ON(!c->parent)) {
711 /* Disconnected dentry must be copied up to index dir */
715 * Mark parent "impure" because it may now contain non-pure
718 err = ovl_set_impure(c->parent, c->destdir);
723 /* Should we copyup with O_TMPFILE or with workdir? */
724 if (S_ISREG(c->stat.mode) && ofs->tmpfile)
725 err = ovl_copy_up_tmpfile(c);
727 err = ovl_copy_up_workdir(c);
732 ovl_set_flag(OVL_INDEX, d_inode(c->dentry));
735 /* Initialize nlink for copy up of disconnected dentry */
736 err = ovl_set_nlink_upper(c->dentry);
738 struct inode *udir = d_inode(c->destdir);
740 /* Restore timestamps on parent (best effort) */
742 ovl_set_timestamps(c->destdir, &c->pstat);
745 ovl_dentry_set_upper_alias(c->dentry);
750 kfree(c->destname.name);
754 static bool ovl_need_meta_copy_up(struct dentry *dentry, umode_t mode,
757 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
759 if (!ofs->config.metacopy)
765 if (flags && ((OPEN_FMODE(flags) & FMODE_WRITE) || (flags & O_TRUNC)))
771 /* Copy up data of an inode which was copied up metadata only in the past. */
772 static int ovl_copy_up_meta_inode_data(struct ovl_copy_up_ctx *c)
774 struct path upperpath, datapath;
776 char *capability = NULL;
777 ssize_t uninitialized_var(cap_size);
779 ovl_path_upper(c->dentry, &upperpath);
780 if (WARN_ON(upperpath.dentry == NULL))
783 ovl_path_lowerdata(c->dentry, &datapath);
784 if (WARN_ON(datapath.dentry == NULL))
788 err = cap_size = ovl_getxattr(upperpath.dentry, XATTR_NAME_CAPS,
790 if (err < 0 && err != -ENODATA)
794 err = ovl_copy_up_data(&datapath, &upperpath, c->stat.size);
799 * Writing to upper file will clear security.capability xattr. We
800 * don't want that to happen for normal copy-up operation.
803 err = ovl_do_setxattr(upperpath.dentry, XATTR_NAME_CAPS,
804 capability, cap_size, 0);
810 err = vfs_removexattr(upperpath.dentry, OVL_XATTR_METACOPY);
814 ovl_set_upperdata(d_inode(c->dentry));
821 static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
825 DEFINE_DELAYED_CALL(done);
826 struct path parentpath;
827 struct ovl_copy_up_ctx ctx = {
830 .workdir = ovl_workdir(dentry),
833 if (WARN_ON(!ctx.workdir))
836 ovl_path_lower(dentry, &ctx.lowerpath);
837 err = vfs_getattr(&ctx.lowerpath, &ctx.stat,
838 STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
842 ctx.metacopy = ovl_need_meta_copy_up(dentry, ctx.stat.mode, flags);
845 ovl_path_upper(parent, &parentpath);
846 ctx.destdir = parentpath.dentry;
847 ctx.destname = dentry->d_name;
849 err = vfs_getattr(&parentpath, &ctx.pstat,
850 STATX_ATIME | STATX_MTIME,
851 AT_STATX_SYNC_AS_STAT);
856 /* maybe truncate regular file. this has no effect on dirs */
860 if (S_ISLNK(ctx.stat.mode)) {
861 ctx.link = vfs_get_link(ctx.lowerpath.dentry, &done);
862 if (IS_ERR(ctx.link))
863 return PTR_ERR(ctx.link);
866 err = ovl_copy_up_start(dentry, flags);
867 /* err < 0: interrupted, err > 0: raced with another copy-up */
872 if (!ovl_dentry_upper(dentry))
873 err = ovl_do_copy_up(&ctx);
874 if (!err && parent && !ovl_dentry_has_upper_alias(dentry))
875 err = ovl_link_up(&ctx);
876 if (!err && ovl_dentry_needs_data_copy_up_locked(dentry, flags))
877 err = ovl_copy_up_meta_inode_data(&ctx);
878 ovl_copy_up_end(dentry);
880 do_delayed_call(&done);
885 int ovl_copy_up_flags(struct dentry *dentry, int flags)
888 const struct cred *old_cred = ovl_override_creds(dentry->d_sb);
889 bool disconnected = (dentry->d_flags & DCACHE_DISCONNECTED);
892 * With NFS export, copy up can get called for a disconnected non-dir.
893 * In this case, we will copy up lower inode to index dir without
894 * linking it to upper dir.
896 if (WARN_ON(disconnected && d_is_dir(dentry)))
901 struct dentry *parent = NULL;
903 if (ovl_already_copied_up(dentry, flags))
907 /* find the topmost dentry not yet copied up */
908 for (; !disconnected;) {
909 parent = dget_parent(next);
911 if (ovl_dentry_upper(parent))
918 err = ovl_copy_up_one(parent, next, flags);
923 revert_creds(old_cred);
928 static bool ovl_open_need_copy_up(struct dentry *dentry, int flags)
930 /* Copy up of disconnected dentry does not set upper alias */
931 if (ovl_already_copied_up(dentry, flags))
934 if (special_file(d_inode(dentry)->i_mode))
937 if (!ovl_open_flags_need_copy_up(flags))
943 int ovl_maybe_copy_up(struct dentry *dentry, int flags)
947 if (ovl_open_need_copy_up(dentry, flags)) {
948 err = ovl_want_write(dentry);
950 err = ovl_copy_up_flags(dentry, flags);
951 ovl_drop_write(dentry);
958 int ovl_copy_up_with_data(struct dentry *dentry)
960 return ovl_copy_up_flags(dentry, O_WRONLY);
963 int ovl_copy_up(struct dentry *dentry)
965 return ovl_copy_up_flags(dentry, 0);