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;
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;
131 old_file = ovl_path_open(old, O_LARGEFILE | O_RDONLY);
132 if (IS_ERR(old_file))
133 return PTR_ERR(old_file);
135 new_file = ovl_path_open(new, O_LARGEFILE | O_WRONLY);
136 if (IS_ERR(new_file)) {
137 error = PTR_ERR(new_file);
141 /* Try to use clone_file_range to clone up within the same fs */
142 cloned = do_clone_file_range(old_file, 0, new_file, 0, len, 0);
145 /* Couldn't clone, so now we try to copy the data */
147 /* FIXME: copy up sparse files efficiently */
149 size_t this_len = OVL_COPY_UP_CHUNK_SIZE;
155 if (signal_pending_state(TASK_KILLABLE, current)) {
160 bytes = do_splice_direct(old_file, &old_pos,
162 this_len, SPLICE_F_MOVE);
167 WARN_ON(old_pos != new_pos);
173 error = vfs_fsync(new_file, 0);
180 static int ovl_set_size(struct dentry *upperdentry, struct kstat *stat)
182 struct iattr attr = {
183 .ia_valid = ATTR_SIZE,
184 .ia_size = stat->size,
187 return notify_change(upperdentry, &attr, NULL);
190 static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat)
192 struct iattr attr = {
194 ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET,
195 .ia_atime = stat->atime,
196 .ia_mtime = stat->mtime,
199 return notify_change(upperdentry, &attr, NULL);
202 int ovl_set_attr(struct dentry *upperdentry, struct kstat *stat)
206 if (!S_ISLNK(stat->mode)) {
207 struct iattr attr = {
208 .ia_valid = ATTR_MODE,
209 .ia_mode = stat->mode,
211 err = notify_change(upperdentry, &attr, NULL);
214 struct iattr attr = {
215 .ia_valid = ATTR_UID | ATTR_GID,
219 err = notify_change(upperdentry, &attr, NULL);
222 ovl_set_timestamps(upperdentry, stat);
227 struct ovl_fh *ovl_encode_real_fh(struct dentry *real, bool is_upper)
230 int fh_type, fh_len, dwords;
232 int buflen = MAX_HANDLE_SZ;
233 uuid_t *uuid = &real->d_sb->s_uuid;
235 buf = kmalloc(buflen, GFP_KERNEL);
237 return ERR_PTR(-ENOMEM);
240 * We encode a non-connectable file handle for non-dir, because we
241 * only need to find the lower inode number and we don't want to pay
242 * the price or reconnecting the dentry.
244 dwords = buflen >> 2;
245 fh_type = exportfs_encode_fh(real, buf, &dwords, 0);
246 buflen = (dwords << 2);
249 if (WARN_ON(fh_type < 0) ||
250 WARN_ON(buflen > MAX_HANDLE_SZ) ||
251 WARN_ON(fh_type == FILEID_INVALID))
254 BUILD_BUG_ON(MAX_HANDLE_SZ + offsetof(struct ovl_fh, fid) > 255);
255 fh_len = offsetof(struct ovl_fh, fid) + buflen;
256 fh = kmalloc(fh_len, GFP_KERNEL);
258 fh = ERR_PTR(-ENOMEM);
262 fh->version = OVL_FH_VERSION;
263 fh->magic = OVL_FH_MAGIC;
265 fh->flags = OVL_FH_FLAG_CPU_ENDIAN;
267 * When we will want to decode an overlay dentry from this handle
268 * and all layers are on the same fs, if we get a disconncted real
269 * dentry when we decode fid, the only way to tell if we should assign
270 * it to upperdentry or to lowerstack is by checking this flag.
273 fh->flags |= OVL_FH_FLAG_PATH_UPPER;
276 memcpy(fh->fid, buf, buflen);
283 int ovl_set_origin(struct dentry *dentry, struct dentry *lower,
284 struct dentry *upper)
286 const struct ovl_fh *fh = NULL;
290 * When lower layer doesn't support export operations store a 'null' fh,
291 * so we can use the overlay.origin xattr to distignuish between a copy
292 * up and a pure upper inode.
294 if (ovl_can_decode_fh(lower->d_sb)) {
295 fh = ovl_encode_real_fh(lower, false);
301 * Do not fail when upper doesn't support xattrs.
303 err = ovl_check_setxattr(dentry, upper, OVL_XATTR_ORIGIN, fh,
304 fh ? fh->len : 0, 0);
310 /* Store file handle of @upper dir in @index dir entry */
311 static int ovl_set_upper_fh(struct dentry *upper, struct dentry *index)
313 const struct ovl_fh *fh;
316 fh = ovl_encode_real_fh(upper, true);
320 err = ovl_do_setxattr(index, OVL_XATTR_UPPER, fh, fh->len, 0);
327 * Create and install index entry.
329 * Caller must hold i_mutex on indexdir.
331 static int ovl_create_index(struct dentry *dentry, struct dentry *origin,
332 struct dentry *upper)
334 struct dentry *indexdir = ovl_indexdir(dentry->d_sb);
335 struct inode *dir = d_inode(indexdir);
336 struct dentry *index = NULL;
337 struct dentry *temp = NULL;
338 struct qstr name = { };
342 * For now this is only used for creating index entry for directories,
343 * because non-dir are copied up directly to index and then hardlinked
346 * TODO: implement create index for non-dir, so we can call it when
347 * encoding file handle for non-dir in case index does not exist.
349 if (WARN_ON(!d_is_dir(dentry)))
352 /* Directory not expected to be indexed before copy up */
353 if (WARN_ON(ovl_test_flag(OVL_INDEX, d_inode(dentry))))
356 err = ovl_get_index_name(origin, &name);
360 temp = ovl_create_temp(indexdir, OVL_CATTR(S_IFDIR | 0));
365 err = ovl_set_upper_fh(upper, temp);
369 index = lookup_one_len(name.name, indexdir, name.len);
371 err = PTR_ERR(index);
373 err = ovl_do_rename(dir, temp, dir, index, 0);
378 ovl_cleanup(dir, temp);
385 struct ovl_copy_up_ctx {
386 struct dentry *parent;
387 struct dentry *dentry;
388 struct path lowerpath;
392 struct dentry *destdir;
393 struct qstr destname;
394 struct dentry *workdir;
400 static int ovl_link_up(struct ovl_copy_up_ctx *c)
403 struct dentry *upper;
404 struct dentry *upperdir = ovl_dentry_upper(c->parent);
405 struct inode *udir = d_inode(upperdir);
407 /* Mark parent "impure" because it may now contain non-pure upper */
408 err = ovl_set_impure(c->parent, upperdir);
412 err = ovl_set_nlink_lower(c->dentry);
416 inode_lock_nested(udir, I_MUTEX_PARENT);
417 upper = lookup_one_len(c->dentry->d_name.name, upperdir,
418 c->dentry->d_name.len);
419 err = PTR_ERR(upper);
420 if (!IS_ERR(upper)) {
421 err = ovl_do_link(ovl_dentry_upper(c->dentry), udir, upper);
425 /* Restore timestamps on parent (best effort) */
426 ovl_set_timestamps(upperdir, &c->pstat);
427 ovl_dentry_set_upper_alias(c->dentry);
434 err = ovl_set_nlink_upper(c->dentry);
439 static int ovl_copy_up_inode(struct ovl_copy_up_ctx *c, struct dentry *temp)
444 * Copy up data first and then xattrs. Writing data after
445 * xattrs will remove security.capability xattr automatically.
447 if (S_ISREG(c->stat.mode) && !c->metacopy) {
448 struct path upperpath, datapath;
450 ovl_path_upper(c->dentry, &upperpath);
451 if (WARN_ON(upperpath.dentry != NULL))
453 upperpath.dentry = temp;
455 ovl_path_lowerdata(c->dentry, &datapath);
456 err = ovl_copy_up_data(&datapath, &upperpath, c->stat.size);
461 err = ovl_copy_xattr(c->lowerpath.dentry, temp);
466 * Store identifier of lower inode in upper inode xattr to
467 * allow lookup of the copy up origin inode.
469 * Don't set origin when we are breaking the association with a lower
473 err = ovl_set_origin(c->dentry, c->lowerpath.dentry, temp);
479 err = ovl_check_setxattr(c->dentry, temp, OVL_XATTR_METACOPY,
480 NULL, 0, -EOPNOTSUPP);
485 inode_lock(temp->d_inode);
487 err = ovl_set_size(temp, &c->stat);
489 err = ovl_set_attr(temp, &c->stat);
490 inode_unlock(temp->d_inode);
495 struct ovl_cu_creds {
496 const struct cred *old;
500 static int ovl_prep_cu_creds(struct dentry *dentry, struct ovl_cu_creds *cc)
504 cc->old = cc->new = NULL;
505 err = security_inode_copy_up(dentry, &cc->new);
510 cc->old = override_creds(cc->new);
515 static void ovl_revert_cu_creds(struct ovl_cu_creds *cc)
518 revert_creds(cc->old);
524 * Copyup using workdir to prepare temp file. Used when copying up directories,
525 * special files or when upper fs doesn't support O_TMPFILE.
527 static int ovl_copy_up_workdir(struct ovl_copy_up_ctx *c)
530 struct inode *udir = d_inode(c->destdir), *wdir = d_inode(c->workdir);
531 struct dentry *temp, *upper;
532 struct ovl_cu_creds cc;
534 struct ovl_cattr cattr = {
535 /* Can't properly set mode on creation because of the umask */
536 .mode = c->stat.mode & S_IFMT,
537 .rdev = c->stat.rdev,
541 err = ovl_lock_rename_workdir(c->workdir, c->destdir);
545 err = ovl_prep_cu_creds(c->dentry, &cc);
549 temp = ovl_create_temp(c->workdir, &cattr);
550 ovl_revert_cu_creds(&cc);
556 err = ovl_copy_up_inode(c, temp);
560 if (S_ISDIR(c->stat.mode) && c->indexed) {
561 err = ovl_create_index(c->dentry, c->lowerpath.dentry, temp);
566 upper = lookup_one_len(c->destname.name, c->destdir, c->destname.len);
567 err = PTR_ERR(upper);
571 err = ovl_do_rename(wdir, temp, udir, upper, 0);
577 ovl_set_upperdata(d_inode(c->dentry));
578 inode = d_inode(c->dentry);
579 ovl_inode_update(inode, temp);
580 if (S_ISDIR(inode->i_mode))
581 ovl_set_flag(OVL_WHITEOUTS, inode);
583 unlock_rename(c->workdir, c->destdir);
588 ovl_cleanup(wdir, temp);
593 /* Copyup using O_TMPFILE which does not require cross dir locking */
594 static int ovl_copy_up_tmpfile(struct ovl_copy_up_ctx *c)
596 struct inode *udir = d_inode(c->destdir);
597 struct dentry *temp, *upper;
598 struct ovl_cu_creds cc;
601 err = ovl_prep_cu_creds(c->dentry, &cc);
605 temp = ovl_do_tmpfile(c->workdir, c->stat.mode);
606 ovl_revert_cu_creds(&cc);
609 return PTR_ERR(temp);
611 err = ovl_copy_up_inode(c, temp);
615 inode_lock_nested(udir, I_MUTEX_PARENT);
617 upper = lookup_one_len(c->destname.name, c->destdir, c->destname.len);
618 err = PTR_ERR(upper);
619 if (!IS_ERR(upper)) {
620 err = ovl_do_link(temp, udir, upper);
629 ovl_set_upperdata(d_inode(c->dentry));
630 ovl_inode_update(d_inode(c->dentry), temp);
640 * Copy up a single dentry
642 * All renames start with copy up of source if necessary. The actual
643 * rename will only proceed once the copy up was successful. Copy up uses
644 * upper parent i_mutex for exclusion. Since rename can change d_parent it
645 * is possible that the copy up will lock the old parent. At that point
646 * the file will have already been copied up anyway.
648 static int ovl_do_copy_up(struct ovl_copy_up_ctx *c)
651 struct ovl_fs *ofs = c->dentry->d_sb->s_fs_info;
652 bool to_index = false;
655 * Indexed non-dir is copied up directly to the index entry and then
656 * hardlinked to upper dir. Indexed dir is copied up to indexdir,
657 * then index entry is created and then copied up dir installed.
658 * Copying dir up to indexdir instead of workdir simplifies locking.
660 if (ovl_need_index(c->dentry)) {
662 if (S_ISDIR(c->stat.mode))
663 c->workdir = ovl_indexdir(c->dentry->d_sb);
668 if (S_ISDIR(c->stat.mode) || c->stat.nlink == 1 || to_index)
672 c->destdir = ovl_indexdir(c->dentry->d_sb);
673 err = ovl_get_index_name(c->lowerpath.dentry, &c->destname);
676 } else if (WARN_ON(!c->parent)) {
677 /* Disconnected dentry must be copied up to index dir */
681 * Mark parent "impure" because it may now contain non-pure
684 err = ovl_set_impure(c->parent, c->destdir);
689 /* Should we copyup with O_TMPFILE or with workdir? */
690 if (S_ISREG(c->stat.mode) && ofs->tmpfile)
691 err = ovl_copy_up_tmpfile(c);
693 err = ovl_copy_up_workdir(c);
698 ovl_set_flag(OVL_INDEX, d_inode(c->dentry));
701 /* Initialize nlink for copy up of disconnected dentry */
702 err = ovl_set_nlink_upper(c->dentry);
704 struct inode *udir = d_inode(c->destdir);
706 /* Restore timestamps on parent (best effort) */
708 ovl_set_timestamps(c->destdir, &c->pstat);
711 ovl_dentry_set_upper_alias(c->dentry);
716 kfree(c->destname.name);
720 static bool ovl_need_meta_copy_up(struct dentry *dentry, umode_t mode,
723 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
725 if (!ofs->config.metacopy)
731 if (flags && ((OPEN_FMODE(flags) & FMODE_WRITE) || (flags & O_TRUNC)))
737 /* Copy up data of an inode which was copied up metadata only in the past. */
738 static int ovl_copy_up_meta_inode_data(struct ovl_copy_up_ctx *c)
740 struct path upperpath, datapath;
742 char *capability = NULL;
743 ssize_t uninitialized_var(cap_size);
745 ovl_path_upper(c->dentry, &upperpath);
746 if (WARN_ON(upperpath.dentry == NULL))
749 ovl_path_lowerdata(c->dentry, &datapath);
750 if (WARN_ON(datapath.dentry == NULL))
754 err = cap_size = ovl_getxattr(upperpath.dentry, XATTR_NAME_CAPS,
756 if (err < 0 && err != -ENODATA)
760 err = ovl_copy_up_data(&datapath, &upperpath, c->stat.size);
765 * Writing to upper file will clear security.capability xattr. We
766 * don't want that to happen for normal copy-up operation.
769 err = ovl_do_setxattr(upperpath.dentry, XATTR_NAME_CAPS,
770 capability, cap_size, 0);
776 err = vfs_removexattr(upperpath.dentry, OVL_XATTR_METACOPY);
780 ovl_set_upperdata(d_inode(c->dentry));
787 static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
791 DEFINE_DELAYED_CALL(done);
792 struct path parentpath;
793 struct ovl_copy_up_ctx ctx = {
796 .workdir = ovl_workdir(dentry),
799 if (WARN_ON(!ctx.workdir))
802 ovl_path_lower(dentry, &ctx.lowerpath);
803 err = vfs_getattr(&ctx.lowerpath, &ctx.stat,
804 STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
808 ctx.metacopy = ovl_need_meta_copy_up(dentry, ctx.stat.mode, flags);
811 ovl_path_upper(parent, &parentpath);
812 ctx.destdir = parentpath.dentry;
813 ctx.destname = dentry->d_name;
815 err = vfs_getattr(&parentpath, &ctx.pstat,
816 STATX_ATIME | STATX_MTIME,
817 AT_STATX_SYNC_AS_STAT);
822 /* maybe truncate regular file. this has no effect on dirs */
826 if (S_ISLNK(ctx.stat.mode)) {
827 ctx.link = vfs_get_link(ctx.lowerpath.dentry, &done);
828 if (IS_ERR(ctx.link))
829 return PTR_ERR(ctx.link);
832 err = ovl_copy_up_start(dentry, flags);
833 /* err < 0: interrupted, err > 0: raced with another copy-up */
838 if (!ovl_dentry_upper(dentry))
839 err = ovl_do_copy_up(&ctx);
840 if (!err && parent && !ovl_dentry_has_upper_alias(dentry))
841 err = ovl_link_up(&ctx);
842 if (!err && ovl_dentry_needs_data_copy_up_locked(dentry, flags))
843 err = ovl_copy_up_meta_inode_data(&ctx);
844 ovl_copy_up_end(dentry);
846 do_delayed_call(&done);
851 int ovl_copy_up_flags(struct dentry *dentry, int flags)
854 const struct cred *old_cred = ovl_override_creds(dentry->d_sb);
855 bool disconnected = (dentry->d_flags & DCACHE_DISCONNECTED);
858 * With NFS export, copy up can get called for a disconnected non-dir.
859 * In this case, we will copy up lower inode to index dir without
860 * linking it to upper dir.
862 if (WARN_ON(disconnected && d_is_dir(dentry)))
867 struct dentry *parent = NULL;
869 if (ovl_already_copied_up(dentry, flags))
873 /* find the topmost dentry not yet copied up */
874 for (; !disconnected;) {
875 parent = dget_parent(next);
877 if (ovl_dentry_upper(parent))
884 err = ovl_copy_up_one(parent, next, flags);
889 revert_creds(old_cred);
894 static bool ovl_open_need_copy_up(struct dentry *dentry, int flags)
896 /* Copy up of disconnected dentry does not set upper alias */
897 if (ovl_already_copied_up(dentry, flags))
900 if (special_file(d_inode(dentry)->i_mode))
903 if (!ovl_open_flags_need_copy_up(flags))
909 int ovl_maybe_copy_up(struct dentry *dentry, int flags)
913 if (ovl_open_need_copy_up(dentry, flags)) {
914 err = ovl_want_write(dentry);
916 err = ovl_copy_up_flags(dentry, flags);
917 ovl_drop_write(dentry);
924 int ovl_copy_up_with_data(struct dentry *dentry)
926 return ovl_copy_up_flags(dentry, O_WRONLY);
929 int ovl_copy_up(struct dentry *dentry)
931 return ovl_copy_up_flags(dentry, 0);