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;
46 int uninitialized_var(error);
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 size = vfs_getxattr(old, name, value, value_size);
85 size = vfs_getxattr(old, name, NULL, 0);
92 if (size > value_size) {
95 new = krealloc(value, size, GFP_KERNEL);
105 error = security_inode_copy_up_xattr(name);
106 if (error < 0 && error != -EOPNOTSUPP)
110 continue; /* Discard */
112 error = vfs_setxattr(new, name, value, size, 0);
122 static int ovl_copy_up_data(struct path *old, struct path *new, loff_t len)
124 struct file *old_file;
125 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 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 /* FIXME: copy up sparse files efficiently */
152 size_t this_len = OVL_COPY_UP_CHUNK_SIZE;
158 if (signal_pending_state(TASK_KILLABLE, current)) {
163 bytes = do_splice_direct(old_file, &old_pos,
165 this_len, SPLICE_F_MOVE);
170 WARN_ON(old_pos != new_pos);
176 error = vfs_fsync(new_file, 0);
183 static int ovl_set_size(struct dentry *upperdentry, struct kstat *stat)
185 struct iattr attr = {
186 .ia_valid = ATTR_SIZE,
187 .ia_size = stat->size,
190 return notify_change(upperdentry, &attr, NULL);
193 static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat)
195 struct iattr attr = {
197 ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET,
198 .ia_atime = stat->atime,
199 .ia_mtime = stat->mtime,
202 return notify_change(upperdentry, &attr, NULL);
205 int ovl_set_attr(struct dentry *upperdentry, struct kstat *stat)
209 if (!S_ISLNK(stat->mode)) {
210 struct iattr attr = {
211 .ia_valid = ATTR_MODE,
212 .ia_mode = stat->mode,
214 err = notify_change(upperdentry, &attr, NULL);
217 struct iattr attr = {
218 .ia_valid = ATTR_UID | ATTR_GID,
222 err = notify_change(upperdentry, &attr, NULL);
225 ovl_set_timestamps(upperdentry, stat);
230 struct ovl_fh *ovl_encode_real_fh(struct dentry *real, bool is_upper)
233 int fh_type, fh_len, dwords;
235 int buflen = MAX_HANDLE_SZ;
236 uuid_t *uuid = &real->d_sb->s_uuid;
238 buf = kmalloc(buflen, GFP_KERNEL);
240 return ERR_PTR(-ENOMEM);
243 * We encode a non-connectable file handle for non-dir, because we
244 * only need to find the lower inode number and we don't want to pay
245 * the price or reconnecting the dentry.
247 dwords = buflen >> 2;
248 fh_type = exportfs_encode_fh(real, buf, &dwords, 0);
249 buflen = (dwords << 2);
252 if (WARN_ON(fh_type < 0) ||
253 WARN_ON(buflen > MAX_HANDLE_SZ) ||
254 WARN_ON(fh_type == FILEID_INVALID))
257 BUILD_BUG_ON(MAX_HANDLE_SZ + offsetof(struct ovl_fh, fid) > 255);
258 fh_len = offsetof(struct ovl_fh, fid) + buflen;
259 fh = kmalloc(fh_len, GFP_KERNEL);
261 fh = ERR_PTR(-ENOMEM);
265 fh->version = OVL_FH_VERSION;
266 fh->magic = OVL_FH_MAGIC;
268 fh->flags = OVL_FH_FLAG_CPU_ENDIAN;
270 * When we will want to decode an overlay dentry from this handle
271 * and all layers are on the same fs, if we get a disconncted real
272 * dentry when we decode fid, the only way to tell if we should assign
273 * it to upperdentry or to lowerstack is by checking this flag.
276 fh->flags |= OVL_FH_FLAG_PATH_UPPER;
279 memcpy(fh->fid, buf, buflen);
286 int ovl_set_origin(struct dentry *dentry, struct dentry *lower,
287 struct dentry *upper)
289 const struct ovl_fh *fh = NULL;
293 * When lower layer doesn't support export operations store a 'null' fh,
294 * so we can use the overlay.origin xattr to distignuish between a copy
295 * up and a pure upper inode.
297 if (ovl_can_decode_fh(lower->d_sb)) {
298 fh = ovl_encode_real_fh(lower, false);
304 * Do not fail when upper doesn't support xattrs.
306 err = ovl_check_setxattr(dentry, upper, OVL_XATTR_ORIGIN, fh,
307 fh ? fh->len : 0, 0);
313 /* Store file handle of @upper dir in @index dir entry */
314 static int ovl_set_upper_fh(struct dentry *upper, struct dentry *index)
316 const struct ovl_fh *fh;
319 fh = ovl_encode_real_fh(upper, true);
323 err = ovl_do_setxattr(index, OVL_XATTR_UPPER, fh, fh->len, 0);
330 * Create and install index entry.
332 * Caller must hold i_mutex on indexdir.
334 static int ovl_create_index(struct dentry *dentry, struct dentry *origin,
335 struct dentry *upper)
337 struct dentry *indexdir = ovl_indexdir(dentry->d_sb);
338 struct inode *dir = d_inode(indexdir);
339 struct dentry *index = NULL;
340 struct dentry *temp = NULL;
341 struct qstr name = { };
345 * For now this is only used for creating index entry for directories,
346 * because non-dir are copied up directly to index and then hardlinked
349 * TODO: implement create index for non-dir, so we can call it when
350 * encoding file handle for non-dir in case index does not exist.
352 if (WARN_ON(!d_is_dir(dentry)))
355 /* Directory not expected to be indexed before copy up */
356 if (WARN_ON(ovl_test_flag(OVL_INDEX, d_inode(dentry))))
359 err = ovl_get_index_name(origin, &name);
363 temp = ovl_create_temp(indexdir, OVL_CATTR(S_IFDIR | 0));
368 err = ovl_set_upper_fh(upper, temp);
372 index = lookup_one_len(name.name, indexdir, name.len);
374 err = PTR_ERR(index);
376 err = ovl_do_rename(dir, temp, dir, index, 0);
381 ovl_cleanup(dir, temp);
388 struct ovl_copy_up_ctx {
389 struct dentry *parent;
390 struct dentry *dentry;
391 struct path lowerpath;
395 struct dentry *destdir;
396 struct qstr destname;
397 struct dentry *workdir;
403 static int ovl_link_up(struct ovl_copy_up_ctx *c)
406 struct dentry *upper;
407 struct dentry *upperdir = ovl_dentry_upper(c->parent);
408 struct inode *udir = d_inode(upperdir);
410 /* Mark parent "impure" because it may now contain non-pure upper */
411 err = ovl_set_impure(c->parent, upperdir);
415 err = ovl_set_nlink_lower(c->dentry);
419 inode_lock_nested(udir, I_MUTEX_PARENT);
420 upper = lookup_one_len(c->dentry->d_name.name, upperdir,
421 c->dentry->d_name.len);
422 err = PTR_ERR(upper);
423 if (!IS_ERR(upper)) {
424 err = ovl_do_link(ovl_dentry_upper(c->dentry), udir, upper);
428 /* Restore timestamps on parent (best effort) */
429 ovl_set_timestamps(upperdir, &c->pstat);
430 ovl_dentry_set_upper_alias(c->dentry);
437 err = ovl_set_nlink_upper(c->dentry);
442 static int ovl_copy_up_inode(struct ovl_copy_up_ctx *c, struct dentry *temp)
446 err = ovl_copy_xattr(c->lowerpath.dentry, temp);
451 * Store identifier of lower inode in upper inode xattr to
452 * allow lookup of the copy up origin inode.
454 * Don't set origin when we are breaking the association with a lower
458 err = ovl_set_origin(c->dentry, c->lowerpath.dentry, temp);
463 if (S_ISREG(c->stat.mode) && !c->metacopy) {
464 struct path upperpath, datapath;
466 ovl_path_upper(c->dentry, &upperpath);
467 BUG_ON(upperpath.dentry != NULL);
468 upperpath.dentry = temp;
470 ovl_path_lowerdata(c->dentry, &datapath);
471 err = ovl_copy_up_data(&datapath, &upperpath, c->stat.size);
477 err = ovl_check_setxattr(c->dentry, temp, OVL_XATTR_METACOPY,
478 NULL, 0, -EOPNOTSUPP);
483 inode_lock(temp->d_inode);
485 err = ovl_set_size(temp, &c->stat);
487 err = ovl_set_attr(temp, &c->stat);
488 inode_unlock(temp->d_inode);
493 struct ovl_cu_creds {
494 const struct cred *old;
498 static int ovl_prep_cu_creds(struct dentry *dentry, struct ovl_cu_creds *cc)
502 cc->old = cc->new = NULL;
503 err = security_inode_copy_up(dentry, &cc->new);
508 cc->old = override_creds(cc->new);
513 static void ovl_revert_cu_creds(struct ovl_cu_creds *cc)
516 revert_creds(cc->old);
522 * Copyup using workdir to prepare temp file. Used when copying up directories,
523 * special files or when upper fs doesn't support O_TMPFILE.
525 static int ovl_copy_up_workdir(struct ovl_copy_up_ctx *c)
528 struct inode *udir = d_inode(c->destdir), *wdir = d_inode(c->workdir);
529 struct dentry *temp, *upper;
530 struct ovl_cu_creds cc;
532 struct ovl_cattr cattr = {
533 /* Can't properly set mode on creation because of the umask */
534 .mode = c->stat.mode & S_IFMT,
535 .rdev = c->stat.rdev,
539 err = ovl_lock_rename_workdir(c->workdir, c->destdir);
543 err = ovl_prep_cu_creds(c->dentry, &cc);
547 temp = ovl_create_temp(c->workdir, &cattr);
548 ovl_revert_cu_creds(&cc);
554 err = ovl_copy_up_inode(c, temp);
558 if (S_ISDIR(c->stat.mode) && c->indexed) {
559 err = ovl_create_index(c->dentry, c->lowerpath.dentry, temp);
564 upper = lookup_one_len(c->destname.name, c->destdir, c->destname.len);
565 err = PTR_ERR(upper);
569 err = ovl_do_rename(wdir, temp, udir, upper, 0);
575 ovl_set_upperdata(d_inode(c->dentry));
576 inode = d_inode(c->dentry);
577 ovl_inode_update(inode, temp);
578 if (S_ISDIR(inode->i_mode))
579 ovl_set_flag(OVL_WHITEOUTS, inode);
581 unlock_rename(c->workdir, c->destdir);
586 ovl_cleanup(wdir, temp);
591 /* Copyup using O_TMPFILE which does not require cross dir locking */
592 static int ovl_copy_up_tmpfile(struct ovl_copy_up_ctx *c)
594 struct inode *udir = d_inode(c->destdir);
595 struct dentry *temp, *upper;
596 struct ovl_cu_creds cc;
599 err = ovl_prep_cu_creds(c->dentry, &cc);
603 temp = ovl_do_tmpfile(c->workdir, c->stat.mode);
604 ovl_revert_cu_creds(&cc);
607 return PTR_ERR(temp);
609 err = ovl_copy_up_inode(c, temp);
613 inode_lock_nested(udir, I_MUTEX_PARENT);
615 upper = lookup_one_len(c->destname.name, c->destdir, c->destname.len);
616 err = PTR_ERR(upper);
617 if (!IS_ERR(upper)) {
618 err = ovl_do_link(temp, udir, upper);
627 ovl_set_upperdata(d_inode(c->dentry));
628 ovl_inode_update(d_inode(c->dentry), temp);
638 * Copy up a single dentry
640 * All renames start with copy up of source if necessary. The actual
641 * rename will only proceed once the copy up was successful. Copy up uses
642 * upper parent i_mutex for exclusion. Since rename can change d_parent it
643 * is possible that the copy up will lock the old parent. At that point
644 * the file will have already been copied up anyway.
646 static int ovl_do_copy_up(struct ovl_copy_up_ctx *c)
649 struct ovl_fs *ofs = c->dentry->d_sb->s_fs_info;
650 bool to_index = false;
653 * Indexed non-dir is copied up directly to the index entry and then
654 * hardlinked to upper dir. Indexed dir is copied up to indexdir,
655 * then index entry is created and then copied up dir installed.
656 * Copying dir up to indexdir instead of workdir simplifies locking.
658 if (ovl_need_index(c->dentry)) {
660 if (S_ISDIR(c->stat.mode))
661 c->workdir = ovl_indexdir(c->dentry->d_sb);
666 if (S_ISDIR(c->stat.mode) || c->stat.nlink == 1 || to_index)
670 c->destdir = ovl_indexdir(c->dentry->d_sb);
671 err = ovl_get_index_name(c->lowerpath.dentry, &c->destname);
674 } else if (WARN_ON(!c->parent)) {
675 /* Disconnected dentry must be copied up to index dir */
679 * Mark parent "impure" because it may now contain non-pure
682 err = ovl_set_impure(c->parent, c->destdir);
687 /* Should we copyup with O_TMPFILE or with workdir? */
688 if (S_ISREG(c->stat.mode) && ofs->tmpfile)
689 err = ovl_copy_up_tmpfile(c);
691 err = ovl_copy_up_workdir(c);
696 ovl_set_flag(OVL_INDEX, d_inode(c->dentry));
699 /* Initialize nlink for copy up of disconnected dentry */
700 err = ovl_set_nlink_upper(c->dentry);
702 struct inode *udir = d_inode(c->destdir);
704 /* Restore timestamps on parent (best effort) */
706 ovl_set_timestamps(c->destdir, &c->pstat);
709 ovl_dentry_set_upper_alias(c->dentry);
714 kfree(c->destname.name);
718 static bool ovl_need_meta_copy_up(struct dentry *dentry, umode_t mode,
721 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
723 if (!ofs->config.metacopy)
729 if (flags && ((OPEN_FMODE(flags) & FMODE_WRITE) || (flags & O_TRUNC)))
735 /* Copy up data of an inode which was copied up metadata only in the past. */
736 static int ovl_copy_up_meta_inode_data(struct ovl_copy_up_ctx *c)
738 struct path upperpath, datapath;
741 ovl_path_upper(c->dentry, &upperpath);
742 if (WARN_ON(upperpath.dentry == NULL))
745 ovl_path_lowerdata(c->dentry, &datapath);
746 if (WARN_ON(datapath.dentry == NULL))
749 err = ovl_copy_up_data(&datapath, &upperpath, c->stat.size);
753 err = vfs_removexattr(upperpath.dentry, OVL_XATTR_METACOPY);
757 ovl_set_upperdata(d_inode(c->dentry));
761 static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
765 DEFINE_DELAYED_CALL(done);
766 struct path parentpath;
767 struct ovl_copy_up_ctx ctx = {
770 .workdir = ovl_workdir(dentry),
773 if (WARN_ON(!ctx.workdir))
776 ovl_path_lower(dentry, &ctx.lowerpath);
777 err = vfs_getattr(&ctx.lowerpath, &ctx.stat,
778 STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
782 ctx.metacopy = ovl_need_meta_copy_up(dentry, ctx.stat.mode, flags);
785 ovl_path_upper(parent, &parentpath);
786 ctx.destdir = parentpath.dentry;
787 ctx.destname = dentry->d_name;
789 err = vfs_getattr(&parentpath, &ctx.pstat,
790 STATX_ATIME | STATX_MTIME,
791 AT_STATX_SYNC_AS_STAT);
796 /* maybe truncate regular file. this has no effect on dirs */
800 if (S_ISLNK(ctx.stat.mode)) {
801 ctx.link = vfs_get_link(ctx.lowerpath.dentry, &done);
802 if (IS_ERR(ctx.link))
803 return PTR_ERR(ctx.link);
806 err = ovl_copy_up_start(dentry, flags);
807 /* err < 0: interrupted, err > 0: raced with another copy-up */
812 if (!ovl_dentry_upper(dentry))
813 err = ovl_do_copy_up(&ctx);
814 if (!err && parent && !ovl_dentry_has_upper_alias(dentry))
815 err = ovl_link_up(&ctx);
816 if (!err && ovl_dentry_needs_data_copy_up_locked(dentry, flags))
817 err = ovl_copy_up_meta_inode_data(&ctx);
818 ovl_copy_up_end(dentry);
820 do_delayed_call(&done);
825 int ovl_copy_up_flags(struct dentry *dentry, int flags)
828 const struct cred *old_cred = ovl_override_creds(dentry->d_sb);
829 bool disconnected = (dentry->d_flags & DCACHE_DISCONNECTED);
832 * With NFS export, copy up can get called for a disconnected non-dir.
833 * In this case, we will copy up lower inode to index dir without
834 * linking it to upper dir.
836 if (WARN_ON(disconnected && d_is_dir(dentry)))
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_open_maybe_copy_up(struct dentry *dentry, unsigned int file_flags)
887 if (ovl_open_need_copy_up(dentry, file_flags)) {
888 err = ovl_want_write(dentry);
890 err = ovl_copy_up_flags(dentry, file_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);