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/namei.h>
20 #include <linux/fdtable.h>
21 #include <linux/ratelimit.h>
22 #include "overlayfs.h"
24 #define OVL_COPY_UP_CHUNK_SIZE (1 << 20)
26 static bool __read_mostly ovl_check_copy_up;
27 module_param_named(check_copy_up, ovl_check_copy_up, bool,
29 MODULE_PARM_DESC(ovl_check_copy_up,
30 "Warn on copy-up when causing process also has a R/O fd open");
32 static int ovl_check_fd(const void *data, struct file *f, unsigned int fd)
34 const struct dentry *dentry = data;
36 if (f->f_inode == d_inode(dentry))
37 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",
38 f, fd, current->pid, current->comm);
43 * Check the fds open by this process and warn if something like the following
44 * scenario is about to occur:
46 * fd1 = open("foo", O_RDONLY);
47 * fd2 = open("foo", O_RDWR);
49 static void ovl_do_check_copy_up(struct dentry *dentry)
51 if (ovl_check_copy_up)
52 iterate_fd(current->files, 0, ovl_check_fd, dentry);
55 int ovl_copy_xattr(struct dentry *old, struct dentry *new)
57 ssize_t list_size, size, value_size = 0;
58 char *buf, *name, *value = NULL;
59 int uninitialized_var(error);
61 if (!old->d_inode->i_op->getxattr ||
62 !new->d_inode->i_op->getxattr)
65 list_size = vfs_listxattr(old, NULL, 0);
67 if (list_size == -EOPNOTSUPP)
72 buf = kzalloc(list_size, GFP_KERNEL);
76 list_size = vfs_listxattr(old, buf, list_size);
82 for (name = buf; name < (buf + list_size); name += strlen(name) + 1) {
84 size = vfs_getxattr(old, name, value, value_size);
86 size = vfs_getxattr(old, name, NULL, 0);
93 if (size > value_size) {
96 new = krealloc(value, size, GFP_KERNEL);
106 error = security_inode_copy_up_xattr(name);
107 if (error < 0 && error != -EOPNOTSUPP)
111 continue; /* Discard */
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 /* FIXME: copy up sparse files efficiently */
146 size_t this_len = OVL_COPY_UP_CHUNK_SIZE;
152 if (signal_pending_state(TASK_KILLABLE, current)) {
157 bytes = do_splice_direct(old_file, &old_pos,
159 this_len, SPLICE_F_MOVE);
164 WARN_ON(old_pos != new_pos);
175 static char *ovl_read_symlink(struct dentry *realdentry)
179 struct inode *inode = realdentry->d_inode;
183 if (!inode->i_op->readlink)
187 buf = (char *) __get_free_page(GFP_KERNEL);
193 /* The cast to a user pointer is valid due to the set_fs() */
194 res = inode->i_op->readlink(realdentry,
195 (char __user *)buf, PAGE_SIZE - 1);
198 free_page((unsigned long) buf);
209 static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat)
211 struct iattr attr = {
213 ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET,
214 .ia_atime = stat->atime,
215 .ia_mtime = stat->mtime,
218 return notify_change(upperdentry, &attr, NULL);
221 int ovl_set_attr(struct dentry *upperdentry, struct kstat *stat)
225 if (!S_ISLNK(stat->mode)) {
226 struct iattr attr = {
227 .ia_valid = ATTR_MODE,
228 .ia_mode = stat->mode,
230 err = notify_change(upperdentry, &attr, NULL);
233 struct iattr attr = {
234 .ia_valid = ATTR_UID | ATTR_GID,
238 err = notify_change(upperdentry, &attr, NULL);
241 ovl_set_timestamps(upperdentry, stat);
246 static int ovl_copy_up_locked(struct dentry *workdir, struct dentry *upperdir,
247 struct dentry *dentry, struct path *lowerpath,
248 struct kstat *stat, const char *link)
250 struct inode *wdir = workdir->d_inode;
251 struct inode *udir = upperdir->d_inode;
252 struct dentry *newdentry = NULL;
253 struct dentry *upper = NULL;
254 umode_t mode = stat->mode;
256 const struct cred *old_creds = NULL;
257 struct cred *new_creds = NULL;
259 newdentry = ovl_lookup_temp(workdir, dentry);
260 err = PTR_ERR(newdentry);
261 if (IS_ERR(newdentry))
264 upper = lookup_one_len(dentry->d_name.name, upperdir,
266 err = PTR_ERR(upper);
270 err = security_inode_copy_up(dentry, &new_creds);
275 old_creds = override_creds(new_creds);
277 /* Can't properly set mode on creation because of the umask */
278 stat->mode &= S_IFMT;
279 err = ovl_create_real(wdir, newdentry, stat, link, NULL, true);
283 revert_creds(old_creds);
290 if (S_ISREG(stat->mode)) {
291 struct path upperpath;
293 ovl_path_upper(dentry, &upperpath);
294 BUG_ON(upperpath.dentry != NULL);
295 upperpath.dentry = newdentry;
297 err = ovl_copy_up_data(lowerpath, &upperpath, stat->size);
302 err = ovl_copy_xattr(lowerpath->dentry, newdentry);
306 inode_lock(newdentry->d_inode);
307 err = ovl_set_attr(newdentry, stat);
308 inode_unlock(newdentry->d_inode);
312 err = ovl_do_rename(wdir, newdentry, udir, upper, 0);
316 ovl_dentry_update(dentry, newdentry);
317 ovl_inode_update(d_inode(dentry), d_inode(newdentry));
321 * Non-directores become opaque when copied up.
323 if (!S_ISDIR(stat->mode))
324 ovl_dentry_set_opaque(dentry, true);
333 ovl_cleanup(wdir, newdentry);
338 * Copy up a single dentry
340 * Directory renames only allowed on "pure upper" (already created on
341 * upper filesystem, never copied up). Directories which are on lower or
342 * are merged may not be renamed. For these -EXDEV is returned and
343 * userspace has to deal with it. This means, when copying up a
344 * directory we can rely on it and ancestors being stable.
346 * Non-directory renames start with copy up of source if necessary. The
347 * actual rename will only proceed once the copy up was successful. Copy
348 * up uses upper parent i_mutex for exclusion. Since rename can change
349 * d_parent it is possible that the copy up will lock the old parent. At
350 * that point the file will have already been copied up anyway.
352 int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
353 struct path *lowerpath, struct kstat *stat)
355 struct dentry *workdir = ovl_workdir(dentry);
358 struct path parentpath;
359 struct dentry *upperdir;
360 struct dentry *upperdentry;
361 const struct cred *old_cred;
364 if (WARN_ON(!workdir))
367 ovl_do_check_copy_up(lowerpath->dentry);
369 ovl_path_upper(parent, &parentpath);
370 upperdir = parentpath.dentry;
372 err = vfs_getattr(&parentpath, &pstat);
376 if (S_ISLNK(stat->mode)) {
377 link = ovl_read_symlink(lowerpath->dentry);
379 return PTR_ERR(link);
382 old_cred = ovl_override_creds(dentry->d_sb);
385 if (lock_rename(workdir, upperdir) != NULL) {
386 pr_err("overlayfs: failed to lock workdir+upperdir\n");
389 upperdentry = ovl_dentry_upper(dentry);
391 /* Raced with another copy-up? Nothing to do, then... */
396 err = ovl_copy_up_locked(workdir, upperdir, dentry, lowerpath,
399 /* Restore timestamps on parent (best effort) */
400 ovl_set_timestamps(upperdir, &pstat);
403 unlock_rename(workdir, upperdir);
404 revert_creds(old_cred);
407 free_page((unsigned long) link);
412 int ovl_copy_up(struct dentry *dentry)
419 struct dentry *parent;
420 struct path lowerpath;
422 enum ovl_path_type type = ovl_path_type(dentry);
424 if (OVL_TYPE_UPPER(type))
428 /* find the topmost dentry not yet copied up */
430 parent = dget_parent(next);
432 type = ovl_path_type(parent);
433 if (OVL_TYPE_UPPER(type))
440 ovl_path_lower(next, &lowerpath);
441 err = vfs_getattr(&lowerpath, &stat);
443 err = ovl_copy_up_one(parent, next, &lowerpath, &stat);