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.h>
19 #include <linux/cred.h>
20 #include <linux/namei.h>
21 #include <linux/fdtable.h>
22 #include <linux/ratelimit.h>
23 #include "overlayfs.h"
25 #define OVL_COPY_UP_CHUNK_SIZE (1 << 20)
27 static bool __read_mostly ovl_check_copy_up;
28 module_param_named(check_copy_up, ovl_check_copy_up, bool,
30 MODULE_PARM_DESC(ovl_check_copy_up,
31 "Warn on copy-up when causing process also has a R/O fd open");
33 static int ovl_check_fd(const void *data, struct file *f, unsigned int fd)
35 const struct dentry *dentry = data;
37 if (file_inode(f) == d_inode(dentry))
38 pr_warn_ratelimited("overlayfs: Warning: Copying up %pD, but open R/O on fd %u which will cease to be coherent [pid=%d %s]\n",
39 f, fd, current->pid, current->comm);
44 * Check the fds open by this process and warn if something like the following
45 * scenario is about to occur:
47 * fd1 = open("foo", O_RDONLY);
48 * fd2 = open("foo", O_RDWR);
50 static void ovl_do_check_copy_up(struct dentry *dentry)
52 if (ovl_check_copy_up)
53 iterate_fd(current->files, 0, ovl_check_fd, dentry);
56 int ovl_copy_xattr(struct dentry *old, struct dentry *new)
58 ssize_t list_size, size, value_size = 0;
59 char *buf, *name, *value = NULL;
60 int uninitialized_var(error);
63 if (!(old->d_inode->i_opflags & IOP_XATTR) ||
64 !(new->d_inode->i_opflags & IOP_XATTR))
67 list_size = vfs_listxattr(old, NULL, 0);
69 if (list_size == -EOPNOTSUPP)
74 buf = kzalloc(list_size, GFP_KERNEL);
78 list_size = vfs_listxattr(old, buf, list_size);
84 for (name = buf; list_size; name += slen) {
85 slen = strnlen(name, list_size) + 1;
87 /* underlying fs providing us with an broken xattr list? */
88 if (WARN_ON(slen > list_size)) {
94 if (ovl_is_private_xattr(name))
97 size = vfs_getxattr(old, name, value, value_size);
99 size = vfs_getxattr(old, name, NULL, 0);
106 if (size > value_size) {
109 new = krealloc(value, size, GFP_KERNEL);
119 error = security_inode_copy_up_xattr(name);
120 if (error < 0 && error != -EOPNOTSUPP)
124 continue; /* Discard */
126 error = vfs_setxattr(new, name, value, size, 0);
136 static int ovl_copy_up_data(struct path *old, struct path *new, loff_t len)
138 struct file *old_file;
139 struct file *new_file;
147 old_file = ovl_path_open(old, O_LARGEFILE | O_RDONLY);
148 if (IS_ERR(old_file))
149 return PTR_ERR(old_file);
151 new_file = ovl_path_open(new, O_LARGEFILE | O_WRONLY);
152 if (IS_ERR(new_file)) {
153 error = PTR_ERR(new_file);
157 /* Try to use clone_file_range to clone up within the same fs */
158 error = vfs_clone_file_range(old_file, 0, new_file, 0, len);
161 /* Couldn't clone, so now we try to copy the data */
164 /* FIXME: copy up sparse files efficiently */
166 size_t this_len = OVL_COPY_UP_CHUNK_SIZE;
172 if (signal_pending_state(TASK_KILLABLE, current)) {
177 bytes = do_splice_direct(old_file, &old_pos,
179 this_len, SPLICE_F_MOVE);
184 WARN_ON(old_pos != new_pos);
190 error = vfs_fsync(new_file, 0);
197 static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat)
199 struct iattr attr = {
201 ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET,
202 .ia_atime = stat->atime,
203 .ia_mtime = stat->mtime,
206 return notify_change(upperdentry, &attr, NULL);
209 int ovl_set_attr(struct dentry *upperdentry, struct kstat *stat)
213 if (!S_ISLNK(stat->mode)) {
214 struct iattr attr = {
215 .ia_valid = ATTR_MODE,
216 .ia_mode = stat->mode,
218 err = notify_change(upperdentry, &attr, NULL);
221 struct iattr attr = {
222 .ia_valid = ATTR_UID | ATTR_GID,
226 err = notify_change(upperdentry, &attr, NULL);
229 ovl_set_timestamps(upperdentry, stat);
234 static int ovl_copy_up_locked(struct dentry *workdir, struct dentry *upperdir,
235 struct dentry *dentry, struct path *lowerpath,
236 struct kstat *stat, const char *link)
238 struct inode *wdir = workdir->d_inode;
239 struct inode *udir = upperdir->d_inode;
240 struct dentry *newdentry = NULL;
241 struct dentry *upper = NULL;
243 const struct cred *old_creds = NULL;
244 struct cred *new_creds = NULL;
245 struct cattr cattr = {
246 /* Can't properly set mode on creation because of the umask */
247 .mode = stat->mode & S_IFMT,
252 newdentry = ovl_lookup_temp(workdir, dentry);
253 err = PTR_ERR(newdentry);
254 if (IS_ERR(newdentry))
257 upper = lookup_one_len(dentry->d_name.name, upperdir,
259 err = PTR_ERR(upper);
263 err = security_inode_copy_up(dentry, &new_creds);
268 old_creds = override_creds(new_creds);
270 err = ovl_create_real(wdir, newdentry, &cattr, NULL, true);
273 revert_creds(old_creds);
280 if (S_ISREG(stat->mode)) {
281 struct path upperpath;
283 ovl_path_upper(dentry, &upperpath);
284 BUG_ON(upperpath.dentry != NULL);
285 upperpath.dentry = newdentry;
287 err = ovl_copy_up_data(lowerpath, &upperpath, stat->size);
292 err = ovl_copy_xattr(lowerpath->dentry, newdentry);
296 inode_lock(newdentry->d_inode);
297 err = ovl_set_attr(newdentry, stat);
298 inode_unlock(newdentry->d_inode);
302 err = ovl_do_rename(wdir, newdentry, udir, upper, 0);
306 ovl_dentry_update(dentry, newdentry);
307 ovl_inode_update(d_inode(dentry), d_inode(newdentry));
317 ovl_cleanup(wdir, newdentry);
322 * Copy up a single dentry
324 * All renames start with copy up of source if necessary. The actual
325 * rename will only proceed once the copy up was successful. Copy up uses
326 * upper parent i_mutex for exclusion. Since rename can change d_parent it
327 * is possible that the copy up will lock the old parent. At that point
328 * the file will have already been copied up anyway.
330 static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
331 struct path *lowerpath, struct kstat *stat)
333 DEFINE_DELAYED_CALL(done);
334 struct dentry *workdir = ovl_workdir(dentry);
337 struct path parentpath;
338 struct dentry *lowerdentry = lowerpath->dentry;
339 struct dentry *upperdir;
340 const char *link = NULL;
342 if (WARN_ON(!workdir))
345 ovl_do_check_copy_up(lowerdentry);
347 ovl_path_upper(parent, &parentpath);
348 upperdir = parentpath.dentry;
350 err = vfs_getattr(&parentpath, &pstat);
354 if (S_ISLNK(stat->mode)) {
355 link = vfs_get_link(lowerdentry, &done);
357 return PTR_ERR(link);
361 if (lock_rename(workdir, upperdir) != NULL) {
362 pr_err("overlayfs: failed to lock workdir+upperdir\n");
365 if (ovl_dentry_upper(dentry)) {
366 /* Raced with another copy-up? Nothing to do, then... */
371 err = ovl_copy_up_locked(workdir, upperdir, dentry, lowerpath,
374 /* Restore timestamps on parent (best effort) */
375 ovl_set_timestamps(upperdir, &pstat);
378 unlock_rename(workdir, upperdir);
379 do_delayed_call(&done);
384 int ovl_copy_up_flags(struct dentry *dentry, int flags)
387 const struct cred *old_cred = ovl_override_creds(dentry->d_sb);
391 struct dentry *parent;
392 struct path lowerpath;
394 enum ovl_path_type type = ovl_path_type(dentry);
396 if (OVL_TYPE_UPPER(type))
400 /* find the topmost dentry not yet copied up */
402 parent = dget_parent(next);
404 type = ovl_path_type(parent);
405 if (OVL_TYPE_UPPER(type))
412 ovl_path_lower(next, &lowerpath);
413 err = vfs_getattr(&lowerpath, &stat);
414 /* maybe truncate regular file. this has no effect on dirs */
418 err = ovl_copy_up_one(parent, next, &lowerpath, &stat);
423 revert_creds(old_cred);
428 int ovl_copy_up(struct dentry *dentry)
430 return ovl_copy_up_flags(dentry, 0);