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.
11 #include <linux/slab.h>
12 #include <linux/file.h>
13 #include <linux/splice.h>
14 #include <linux/xattr.h>
15 #include <linux/security.h>
16 #include <linux/uaccess.h>
17 #include <linux/sched.h>
18 #include <linux/namei.h>
19 #include "overlayfs.h"
21 #define OVL_COPY_UP_CHUNK_SIZE (1 << 20)
23 int ovl_copy_xattr(struct dentry *old, struct dentry *new)
25 ssize_t list_size, size, value_size = 0;
26 char *buf, *name, *value = NULL;
27 int uninitialized_var(error);
30 if (!old->d_inode->i_op->getxattr ||
31 !new->d_inode->i_op->getxattr)
34 list_size = vfs_listxattr(old, NULL, 0);
36 if (list_size == -EOPNOTSUPP)
41 buf = kzalloc(list_size, GFP_KERNEL);
45 list_size = vfs_listxattr(old, buf, list_size);
51 for (name = buf; list_size; name += slen) {
52 slen = strnlen(name, list_size) + 1;
54 /* underlying fs providing us with an broken xattr list? */
55 if (WARN_ON(slen > list_size)) {
61 if (ovl_is_private_xattr(name))
64 size = vfs_getxattr(old, name, value, value_size);
66 size = vfs_getxattr(old, name, NULL, 0);
73 if (size > value_size) {
76 new = krealloc(value, size, GFP_KERNEL);
86 error = vfs_setxattr(new, name, value, size, 0);
96 static int ovl_copy_up_data(struct path *old, struct path *new, loff_t len)
98 struct file *old_file;
99 struct file *new_file;
107 old_file = ovl_path_open(old, O_LARGEFILE | O_RDONLY);
108 if (IS_ERR(old_file))
109 return PTR_ERR(old_file);
111 new_file = ovl_path_open(new, O_LARGEFILE | O_WRONLY);
112 if (IS_ERR(new_file)) {
113 error = PTR_ERR(new_file);
117 /* FIXME: copy up sparse files efficiently */
119 size_t this_len = OVL_COPY_UP_CHUNK_SIZE;
125 if (signal_pending_state(TASK_KILLABLE, current)) {
130 bytes = do_splice_direct(old_file, &old_pos,
132 this_len, SPLICE_F_MOVE);
137 WARN_ON(old_pos != new_pos);
148 static char *ovl_read_symlink(struct dentry *realdentry)
152 struct inode *inode = realdentry->d_inode;
156 if (!inode->i_op->readlink)
160 buf = (char *) __get_free_page(GFP_KERNEL);
166 /* The cast to a user pointer is valid due to the set_fs() */
167 res = inode->i_op->readlink(realdentry,
168 (char __user *)buf, PAGE_SIZE - 1);
171 free_page((unsigned long) buf);
182 static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat)
184 struct iattr attr = {
186 ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET,
187 .ia_atime = stat->atime,
188 .ia_mtime = stat->mtime,
191 return notify_change(upperdentry, &attr, NULL);
194 int ovl_set_attr(struct dentry *upperdentry, struct kstat *stat)
198 if (!S_ISLNK(stat->mode)) {
199 struct iattr attr = {
200 .ia_valid = ATTR_MODE,
201 .ia_mode = stat->mode,
203 err = notify_change(upperdentry, &attr, NULL);
206 struct iattr attr = {
207 .ia_valid = ATTR_UID | ATTR_GID,
211 err = notify_change(upperdentry, &attr, NULL);
214 ovl_set_timestamps(upperdentry, stat);
219 static int ovl_copy_up_locked(struct dentry *workdir, struct dentry *upperdir,
220 struct dentry *dentry, struct path *lowerpath,
221 struct kstat *stat, const char *link)
223 struct inode *wdir = workdir->d_inode;
224 struct inode *udir = upperdir->d_inode;
225 struct dentry *newdentry = NULL;
226 struct dentry *upper = NULL;
227 umode_t mode = stat->mode;
230 newdentry = ovl_lookup_temp(workdir, dentry);
231 err = PTR_ERR(newdentry);
232 if (IS_ERR(newdentry))
235 upper = lookup_one_len(dentry->d_name.name, upperdir,
237 err = PTR_ERR(upper);
241 /* Can't properly set mode on creation because of the umask */
242 stat->mode &= S_IFMT;
243 err = ovl_create_real(wdir, newdentry, stat, link, NULL, true);
248 if (S_ISREG(stat->mode)) {
249 struct path upperpath;
250 ovl_path_upper(dentry, &upperpath);
251 BUG_ON(upperpath.dentry != NULL);
252 upperpath.dentry = newdentry;
254 err = ovl_copy_up_data(lowerpath, &upperpath, stat->size);
259 err = ovl_copy_xattr(lowerpath->dentry, newdentry);
263 mutex_lock(&newdentry->d_inode->i_mutex);
264 err = ovl_set_attr(newdentry, stat);
265 mutex_unlock(&newdentry->d_inode->i_mutex);
269 err = ovl_do_rename(wdir, newdentry, udir, upper, 0);
273 ovl_dentry_update(dentry, newdentry);
277 * Non-directores become opaque when copied up.
279 if (!S_ISDIR(stat->mode))
280 ovl_dentry_set_opaque(dentry, true);
289 ovl_cleanup(wdir, newdentry);
294 * Copy up a single dentry
296 * Directory renames only allowed on "pure upper" (already created on
297 * upper filesystem, never copied up). Directories which are on lower or
298 * are merged may not be renamed. For these -EXDEV is returned and
299 * userspace has to deal with it. This means, when copying up a
300 * directory we can rely on it and ancestors being stable.
302 * Non-directory renames start with copy up of source if necessary. The
303 * actual rename will only proceed once the copy up was successful. Copy
304 * up uses upper parent i_mutex for exclusion. Since rename can change
305 * d_parent it is possible that the copy up will lock the old parent. At
306 * that point the file will have already been copied up anyway.
308 int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
309 struct path *lowerpath, struct kstat *stat)
311 struct dentry *workdir = ovl_workdir(dentry);
314 struct path parentpath;
315 struct dentry *upperdir;
316 struct dentry *upperdentry;
317 const struct cred *old_cred;
318 struct cred *override_cred;
321 if (WARN_ON(!workdir))
324 ovl_path_upper(parent, &parentpath);
325 upperdir = parentpath.dentry;
327 err = vfs_getattr(&parentpath, &pstat);
331 if (S_ISLNK(stat->mode)) {
332 link = ovl_read_symlink(lowerpath->dentry);
334 return PTR_ERR(link);
338 override_cred = prepare_creds();
342 override_cred->fsuid = stat->uid;
343 override_cred->fsgid = stat->gid;
345 * CAP_SYS_ADMIN for copying up extended attributes
346 * CAP_DAC_OVERRIDE for create
347 * CAP_FOWNER for chmod, timestamp update
348 * CAP_FSETID for chmod
349 * CAP_CHOWN for chown
350 * CAP_MKNOD for mknod
352 cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
353 cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
354 cap_raise(override_cred->cap_effective, CAP_FOWNER);
355 cap_raise(override_cred->cap_effective, CAP_FSETID);
356 cap_raise(override_cred->cap_effective, CAP_CHOWN);
357 cap_raise(override_cred->cap_effective, CAP_MKNOD);
358 old_cred = override_creds(override_cred);
361 if (lock_rename(workdir, upperdir) != NULL) {
362 pr_err("overlayfs: failed to lock workdir+upperdir\n");
365 upperdentry = ovl_dentry_upper(dentry);
367 /* Raced with another copy-up? Nothing to do, then... */
372 err = ovl_copy_up_locked(workdir, upperdir, dentry, lowerpath,
375 /* Restore timestamps on parent (best effort) */
376 ovl_set_timestamps(upperdir, &pstat);
379 unlock_rename(workdir, upperdir);
380 revert_creds(old_cred);
381 put_cred(override_cred);
385 free_page((unsigned long) link);
390 int ovl_copy_up(struct dentry *dentry)
397 struct dentry *parent;
398 struct path lowerpath;
400 enum ovl_path_type type = ovl_path_type(dentry);
402 if (OVL_TYPE_UPPER(type))
406 /* find the topmost dentry not yet copied up */
408 parent = dget_parent(next);
410 type = ovl_path_type(parent);
411 if (OVL_TYPE_UPPER(type))
418 ovl_path_lower(next, &lowerpath);
419 err = vfs_getattr(&lowerpath, &stat);
421 err = ovl_copy_up_one(parent, next, &lowerpath, &stat);