3 * Copyright (C) 2011 Novell Inc.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
10 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <linux/file.h>
14 #include <linux/splice.h>
15 #include <linux/xattr.h>
16 #include <linux/security.h>
17 #include <linux/uaccess.h>
18 #include <linux/sched/signal.h>
19 #include <linux/cred.h>
20 #include <linux/namei.h>
21 #include <linux/fdtable.h>
22 #include <linux/ratelimit.h>
23 #include <linux/exportfs.h>
24 #include "overlayfs.h"
26 #define OVL_COPY_UP_CHUNK_SIZE (1 << 20)
28 static int ovl_ccup_set(const char *buf, const struct kernel_param *param)
30 pr_warn("overlayfs: \"check_copy_up\" module option is obsolete\n");
34 static int ovl_ccup_get(char *buf, const struct kernel_param *param)
36 return sprintf(buf, "N\n");
39 module_param_call(check_copy_up, ovl_ccup_set, ovl_ccup_get, NULL, 0644);
40 MODULE_PARM_DESC(ovl_check_copy_up, "Obsolete; does nothing");
42 int ovl_copy_xattr(struct dentry *old, struct dentry *new)
44 ssize_t list_size, size, value_size = 0;
45 char *buf, *name, *value = NULL;
49 if (!(old->d_inode->i_opflags & IOP_XATTR) ||
50 !(new->d_inode->i_opflags & IOP_XATTR))
53 list_size = vfs_listxattr(old, NULL, 0);
55 if (list_size == -EOPNOTSUPP)
60 buf = kzalloc(list_size, GFP_KERNEL);
64 list_size = vfs_listxattr(old, buf, list_size);
70 for (name = buf; list_size; name += slen) {
71 slen = strnlen(name, list_size) + 1;
73 /* underlying fs providing us with an broken xattr list? */
74 if (WARN_ON(slen > list_size)) {
80 if (ovl_is_private_xattr(name))
83 error = security_inode_copy_up_xattr(name);
84 if (error < 0 && error != -EOPNOTSUPP)
88 continue; /* Discard */
91 size = vfs_getxattr(old, name, value, value_size);
93 size = vfs_getxattr(old, name, NULL, 0);
100 if (size > value_size) {
103 new = krealloc(value, size, GFP_KERNEL);
113 error = vfs_setxattr(new, name, value, size, 0);
123 static int ovl_copy_up_data(struct path *old, struct path *new, loff_t len)
125 struct file *old_file;
126 struct file *new_file;
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 error = do_clone_file_range(old_file, 0, new_file, 0, len);
148 /* Couldn't clone, so now we try to copy the data */
151 /* FIXME: copy up sparse files efficiently */
153 size_t this_len = OVL_COPY_UP_CHUNK_SIZE;
159 if (signal_pending_state(TASK_KILLABLE, current)) {
164 bytes = do_splice_direct(old_file, &old_pos,
166 this_len, SPLICE_F_MOVE);
171 WARN_ON(old_pos != new_pos);
177 error = vfs_fsync(new_file, 0);
184 static int ovl_set_size(struct dentry *upperdentry, struct kstat *stat)
186 struct iattr attr = {
187 .ia_valid = ATTR_SIZE,
188 .ia_size = stat->size,
191 return notify_change(upperdentry, &attr, NULL);
194 static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat)
196 struct iattr attr = {
198 ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET,
199 .ia_atime = stat->atime,
200 .ia_mtime = stat->mtime,
203 return notify_change(upperdentry, &attr, NULL);
206 int ovl_set_attr(struct dentry *upperdentry, struct kstat *stat)
210 if (!S_ISLNK(stat->mode)) {
211 struct iattr attr = {
212 .ia_valid = ATTR_MODE,
213 .ia_mode = stat->mode,
215 err = notify_change(upperdentry, &attr, NULL);
218 struct iattr attr = {
219 .ia_valid = ATTR_UID | ATTR_GID,
223 err = notify_change(upperdentry, &attr, NULL);
226 ovl_set_timestamps(upperdentry, stat);
231 struct ovl_fh *ovl_encode_real_fh(struct dentry *real, bool is_upper)
234 int fh_type, fh_len, dwords;
236 int buflen = MAX_HANDLE_SZ;
237 uuid_t *uuid = &real->d_sb->s_uuid;
239 buf = kmalloc(buflen, GFP_KERNEL);
241 return ERR_PTR(-ENOMEM);
244 * We encode a non-connectable file handle for non-dir, because we
245 * only need to find the lower inode number and we don't want to pay
246 * the price or reconnecting the dentry.
248 dwords = buflen >> 2;
249 fh_type = exportfs_encode_fh(real, buf, &dwords, 0);
250 buflen = (dwords << 2);
253 if (WARN_ON(fh_type < 0) ||
254 WARN_ON(buflen > MAX_HANDLE_SZ) ||
255 WARN_ON(fh_type == FILEID_INVALID))
258 BUILD_BUG_ON(MAX_HANDLE_SZ + offsetof(struct ovl_fh, fid) > 255);
259 fh_len = offsetof(struct ovl_fh, fid) + buflen;
260 fh = kmalloc(fh_len, GFP_KERNEL);
262 fh = ERR_PTR(-ENOMEM);
266 fh->version = OVL_FH_VERSION;
267 fh->magic = OVL_FH_MAGIC;
269 fh->flags = OVL_FH_FLAG_CPU_ENDIAN;
271 * When we will want to decode an overlay dentry from this handle
272 * and all layers are on the same fs, if we get a disconncted real
273 * dentry when we decode fid, the only way to tell if we should assign
274 * it to upperdentry or to lowerstack is by checking this flag.
277 fh->flags |= OVL_FH_FLAG_PATH_UPPER;
280 memcpy(fh->fid, buf, buflen);
287 int ovl_set_origin(struct dentry *dentry, struct dentry *lower,
288 struct dentry *upper)
290 const struct ovl_fh *fh = NULL;
294 * When lower layer doesn't support export operations store a 'null' fh,
295 * so we can use the overlay.origin xattr to distignuish between a copy
296 * up and a pure upper inode.
298 if (ovl_can_decode_fh(lower->d_sb)) {
299 fh = ovl_encode_real_fh(lower, false);
305 * Do not fail when upper doesn't support xattrs.
307 err = ovl_check_setxattr(dentry, upper, OVL_XATTR_ORIGIN, fh,
308 fh ? fh->len : 0, 0);
314 /* Store file handle of @upper dir in @index dir entry */
315 static int ovl_set_upper_fh(struct dentry *upper, struct dentry *index)
317 const struct ovl_fh *fh;
320 fh = ovl_encode_real_fh(upper, true);
324 err = ovl_do_setxattr(index, OVL_XATTR_UPPER, fh, fh->len, 0);
331 * Create and install index entry.
333 * Caller must hold i_mutex on indexdir.
335 static int ovl_create_index(struct dentry *dentry, struct dentry *origin,
336 struct dentry *upper)
338 struct dentry *indexdir = ovl_indexdir(dentry->d_sb);
339 struct inode *dir = d_inode(indexdir);
340 struct dentry *index = NULL;
341 struct dentry *temp = NULL;
342 struct qstr name = { };
346 * For now this is only used for creating index entry for directories,
347 * because non-dir are copied up directly to index and then hardlinked
350 * TODO: implement create index for non-dir, so we can call it when
351 * encoding file handle for non-dir in case index does not exist.
353 if (WARN_ON(!d_is_dir(dentry)))
356 /* Directory not expected to be indexed before copy up */
357 if (WARN_ON(ovl_test_flag(OVL_INDEX, d_inode(dentry))))
360 err = ovl_get_index_name(origin, &name);
364 temp = ovl_create_temp(indexdir, OVL_CATTR(S_IFDIR | 0));
369 err = ovl_set_upper_fh(upper, temp);
373 index = lookup_one_len(name.name, indexdir, name.len);
375 err = PTR_ERR(index);
377 err = ovl_do_rename(dir, temp, dir, index, 0);
382 ovl_cleanup(dir, temp);
389 struct ovl_copy_up_ctx {
390 struct dentry *parent;
391 struct dentry *dentry;
392 struct path lowerpath;
396 struct dentry *destdir;
397 struct qstr destname;
398 struct dentry *workdir;
405 static int ovl_link_up(struct ovl_copy_up_ctx *c)
408 struct dentry *upper;
409 struct dentry *upperdir = ovl_dentry_upper(c->parent);
410 struct inode *udir = d_inode(upperdir);
412 /* Mark parent "impure" because it may now contain non-pure upper */
413 err = ovl_set_impure(c->parent, upperdir);
417 err = ovl_set_nlink_lower(c->dentry);
421 inode_lock_nested(udir, I_MUTEX_PARENT);
422 upper = lookup_one_len(c->dentry->d_name.name, upperdir,
423 c->dentry->d_name.len);
424 err = PTR_ERR(upper);
425 if (!IS_ERR(upper)) {
426 err = ovl_do_link(ovl_dentry_upper(c->dentry), udir, upper);
430 /* Restore timestamps on parent (best effort) */
431 ovl_set_timestamps(upperdir, &c->pstat);
432 ovl_dentry_set_upper_alias(c->dentry);
439 err = ovl_set_nlink_upper(c->dentry);
444 static int ovl_install_temp(struct ovl_copy_up_ctx *c, struct dentry *temp,
445 struct dentry **newdentry)
448 struct dentry *upper;
449 struct inode *udir = d_inode(c->destdir);
451 upper = lookup_one_len(c->destname.name, c->destdir, c->destname.len);
453 return PTR_ERR(upper);
456 err = ovl_do_link(temp, udir, upper);
458 err = ovl_do_rename(d_inode(c->workdir), temp, udir, upper, 0);
461 *newdentry = dget(c->tmpfile ? upper : temp);
467 static struct dentry *ovl_get_tmpfile(struct ovl_copy_up_ctx *c)
471 const struct cred *old_creds = NULL;
472 struct cred *new_creds = NULL;
473 struct ovl_cattr cattr = {
474 /* Can't properly set mode on creation because of the umask */
475 .mode = c->stat.mode & S_IFMT,
476 .rdev = c->stat.rdev,
480 err = security_inode_copy_up(c->dentry, &new_creds);
486 old_creds = override_creds(new_creds);
489 temp = ovl_do_tmpfile(c->workdir, c->stat.mode);
491 temp = ovl_create_temp(c->workdir, &cattr);
494 revert_creds(old_creds);
501 static int ovl_copy_up_inode(struct ovl_copy_up_ctx *c, struct dentry *temp)
506 * Copy up data first and then xattrs. Writing data after
507 * xattrs will remove security.capability xattr automatically.
509 if (S_ISREG(c->stat.mode) && !c->metacopy) {
510 struct path upperpath, datapath;
512 ovl_path_upper(c->dentry, &upperpath);
513 if (WARN_ON(upperpath.dentry != NULL))
515 upperpath.dentry = temp;
517 ovl_path_lowerdata(c->dentry, &datapath);
518 err = ovl_copy_up_data(&datapath, &upperpath, c->stat.size);
523 err = ovl_copy_xattr(c->lowerpath.dentry, temp);
528 * Store identifier of lower inode in upper inode xattr to
529 * allow lookup of the copy up origin inode.
531 * Don't set origin when we are breaking the association with a lower
535 err = ovl_set_origin(c->dentry, c->lowerpath.dentry, temp);
541 err = ovl_check_setxattr(c->dentry, temp, OVL_XATTR_METACOPY,
542 NULL, 0, -EOPNOTSUPP);
547 inode_lock(temp->d_inode);
549 err = ovl_set_size(temp, &c->stat);
551 err = ovl_set_attr(temp, &c->stat);
552 inode_unlock(temp->d_inode);
557 static int ovl_copy_up_locked(struct ovl_copy_up_ctx *c)
559 struct inode *udir = c->destdir->d_inode;
561 struct dentry *newdentry = NULL;
565 temp = ovl_get_tmpfile(c);
567 return PTR_ERR(temp);
569 err = ovl_copy_up_inode(c, temp);
573 if (S_ISDIR(c->stat.mode) && c->indexed) {
574 err = ovl_create_index(c->dentry, c->lowerpath.dentry, temp);
580 inode_lock_nested(udir, I_MUTEX_PARENT);
581 err = ovl_install_temp(c, temp, &newdentry);
584 err = ovl_install_temp(c, temp, &newdentry);
590 ovl_set_upperdata(d_inode(c->dentry));
591 inode = d_inode(c->dentry);
592 ovl_inode_update(inode, newdentry);
593 if (S_ISDIR(inode->i_mode))
594 ovl_set_flag(OVL_WHITEOUTS, inode);
597 if (err && !c->tmpfile)
598 ovl_cleanup(d_inode(c->workdir), temp);
605 * Copy up a single dentry
607 * All renames start with copy up of source if necessary. The actual
608 * rename will only proceed once the copy up was successful. Copy up uses
609 * upper parent i_mutex for exclusion. Since rename can change d_parent it
610 * is possible that the copy up will lock the old parent. At that point
611 * the file will have already been copied up anyway.
613 static int ovl_do_copy_up(struct ovl_copy_up_ctx *c)
616 struct ovl_fs *ofs = c->dentry->d_sb->s_fs_info;
617 bool to_index = false;
620 * Indexed non-dir is copied up directly to the index entry and then
621 * hardlinked to upper dir. Indexed dir is copied up to indexdir,
622 * then index entry is created and then copied up dir installed.
623 * Copying dir up to indexdir instead of workdir simplifies locking.
625 if (ovl_need_index(c->dentry)) {
627 if (S_ISDIR(c->stat.mode))
628 c->workdir = ovl_indexdir(c->dentry->d_sb);
633 if (S_ISDIR(c->stat.mode) || c->stat.nlink == 1 || to_index)
637 c->destdir = ovl_indexdir(c->dentry->d_sb);
638 err = ovl_get_index_name(c->lowerpath.dentry, &c->destname);
641 } else if (WARN_ON(!c->parent)) {
642 /* Disconnected dentry must be copied up to index dir */
646 * Mark parent "impure" because it may now contain non-pure
649 err = ovl_set_impure(c->parent, c->destdir);
654 /* Should we copyup with O_TMPFILE or with workdir? */
655 if (S_ISREG(c->stat.mode) && ofs->tmpfile) {
657 err = ovl_copy_up_locked(c);
659 err = ovl_lock_rename_workdir(c->workdir, c->destdir);
661 err = ovl_copy_up_locked(c);
662 unlock_rename(c->workdir, c->destdir);
671 ovl_set_flag(OVL_INDEX, d_inode(c->dentry));
674 /* Initialize nlink for copy up of disconnected dentry */
675 err = ovl_set_nlink_upper(c->dentry);
677 struct inode *udir = d_inode(c->destdir);
679 /* Restore timestamps on parent (best effort) */
681 ovl_set_timestamps(c->destdir, &c->pstat);
684 ovl_dentry_set_upper_alias(c->dentry);
689 kfree(c->destname.name);
693 static bool ovl_need_meta_copy_up(struct dentry *dentry, umode_t mode,
696 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
698 if (!ofs->config.metacopy)
704 if (flags && ((OPEN_FMODE(flags) & FMODE_WRITE) || (flags & O_TRUNC)))
710 /* Copy up data of an inode which was copied up metadata only in the past. */
711 static int ovl_copy_up_meta_inode_data(struct ovl_copy_up_ctx *c)
713 struct path upperpath, datapath;
715 char *capability = NULL;
716 ssize_t uninitialized_var(cap_size);
718 ovl_path_upper(c->dentry, &upperpath);
719 if (WARN_ON(upperpath.dentry == NULL))
722 ovl_path_lowerdata(c->dentry, &datapath);
723 if (WARN_ON(datapath.dentry == NULL))
727 err = cap_size = ovl_getxattr(upperpath.dentry, XATTR_NAME_CAPS,
729 if (err < 0 && err != -ENODATA)
733 err = ovl_copy_up_data(&datapath, &upperpath, c->stat.size);
738 * Writing to upper file will clear security.capability xattr. We
739 * don't want that to happen for normal copy-up operation.
742 err = ovl_do_setxattr(upperpath.dentry, XATTR_NAME_CAPS,
743 capability, cap_size, 0);
749 err = vfs_removexattr(upperpath.dentry, OVL_XATTR_METACOPY);
753 ovl_set_upperdata(d_inode(c->dentry));
760 static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
764 DEFINE_DELAYED_CALL(done);
765 struct path parentpath;
766 struct ovl_copy_up_ctx ctx = {
769 .workdir = ovl_workdir(dentry),
772 if (WARN_ON(!ctx.workdir))
775 ovl_path_lower(dentry, &ctx.lowerpath);
776 err = vfs_getattr(&ctx.lowerpath, &ctx.stat,
777 STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
781 ctx.metacopy = ovl_need_meta_copy_up(dentry, ctx.stat.mode, flags);
784 ovl_path_upper(parent, &parentpath);
785 ctx.destdir = parentpath.dentry;
786 ctx.destname = dentry->d_name;
788 err = vfs_getattr(&parentpath, &ctx.pstat,
789 STATX_ATIME | STATX_MTIME,
790 AT_STATX_SYNC_AS_STAT);
795 /* maybe truncate regular file. this has no effect on dirs */
799 if (S_ISLNK(ctx.stat.mode)) {
800 ctx.link = vfs_get_link(ctx.lowerpath.dentry, &done);
801 if (IS_ERR(ctx.link))
802 return PTR_ERR(ctx.link);
805 err = ovl_copy_up_start(dentry, flags);
806 /* err < 0: interrupted, err > 0: raced with another copy-up */
811 if (!ovl_dentry_upper(dentry))
812 err = ovl_do_copy_up(&ctx);
813 if (!err && parent && !ovl_dentry_has_upper_alias(dentry))
814 err = ovl_link_up(&ctx);
815 if (!err && ovl_dentry_needs_data_copy_up_locked(dentry, flags))
816 err = ovl_copy_up_meta_inode_data(&ctx);
817 ovl_copy_up_end(dentry);
819 do_delayed_call(&done);
824 int ovl_copy_up_flags(struct dentry *dentry, int flags)
827 const struct cred *old_cred;
828 bool disconnected = (dentry->d_flags & DCACHE_DISCONNECTED);
831 * With NFS export, copy up can get called for a disconnected non-dir.
832 * In this case, we will copy up lower inode to index dir without
833 * linking it to upper dir.
835 if (WARN_ON(disconnected && d_is_dir(dentry)))
838 old_cred = ovl_override_creds(dentry->d_sb);
841 struct dentry *parent = NULL;
843 if (ovl_already_copied_up(dentry, flags))
847 /* find the topmost dentry not yet copied up */
848 for (; !disconnected;) {
849 parent = dget_parent(next);
851 if (ovl_dentry_upper(parent))
858 err = ovl_copy_up_one(parent, next, flags);
863 revert_creds(old_cred);
868 static bool ovl_open_need_copy_up(struct dentry *dentry, int flags)
870 /* Copy up of disconnected dentry does not set upper alias */
871 if (ovl_already_copied_up(dentry, flags))
874 if (special_file(d_inode(dentry)->i_mode))
877 if (!ovl_open_flags_need_copy_up(flags))
883 int ovl_maybe_copy_up(struct dentry *dentry, int flags)
887 if (ovl_open_need_copy_up(dentry, flags)) {
888 err = ovl_want_write(dentry);
890 err = ovl_copy_up_flags(dentry, flags);
891 ovl_drop_write(dentry);
898 int ovl_copy_up_with_data(struct dentry *dentry)
900 return ovl_copy_up_flags(dentry, O_WRONLY);
903 int ovl_copy_up(struct dentry *dentry)
905 return ovl_copy_up_flags(dentry, 0);