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
;
26 char *buf
, *name
, *value
;
29 if (!old
->d_inode
->i_op
->getxattr
||
30 !new->d_inode
->i_op
->getxattr
)
33 list_size
= vfs_listxattr(old
, NULL
, 0);
35 if (list_size
== -EOPNOTSUPP
)
40 buf
= kzalloc(list_size
, GFP_KERNEL
);
45 value
= kmalloc(XATTR_SIZE_MAX
, GFP_KERNEL
);
49 list_size
= vfs_listxattr(old
, buf
, list_size
);
55 for (name
= buf
; name
< (buf
+ list_size
); name
+= strlen(name
) + 1) {
56 size
= vfs_getxattr(old
, name
, value
, XATTR_SIZE_MAX
);
61 error
= vfs_setxattr(new, name
, value
, size
, 0);
73 static int ovl_copy_up_data(struct path
*old
, struct path
*new, loff_t len
)
75 struct file
*old_file
;
76 struct file
*new_file
;
84 old_file
= ovl_path_open(old
, O_LARGEFILE
| O_RDONLY
);
86 return PTR_ERR(old_file
);
88 new_file
= ovl_path_open(new, O_LARGEFILE
| O_WRONLY
);
89 if (IS_ERR(new_file
)) {
90 error
= PTR_ERR(new_file
);
94 /* FIXME: copy up sparse files efficiently */
96 size_t this_len
= OVL_COPY_UP_CHUNK_SIZE
;
102 if (signal_pending_state(TASK_KILLABLE
, current
)) {
107 bytes
= do_splice_direct(old_file
, &old_pos
,
109 this_len
, SPLICE_F_MOVE
);
114 WARN_ON(old_pos
!= new_pos
);
125 static char *ovl_read_symlink(struct dentry
*realdentry
)
129 struct inode
*inode
= realdentry
->d_inode
;
133 if (!inode
->i_op
->readlink
)
137 buf
= (char *) __get_free_page(GFP_KERNEL
);
143 /* The cast to a user pointer is valid due to the set_fs() */
144 res
= inode
->i_op
->readlink(realdentry
,
145 (char __user
*)buf
, PAGE_SIZE
- 1);
148 free_page((unsigned long) buf
);
159 static int ovl_set_timestamps(struct dentry
*upperdentry
, struct kstat
*stat
)
161 struct iattr attr
= {
163 ATTR_ATIME
| ATTR_MTIME
| ATTR_ATIME_SET
| ATTR_MTIME_SET
,
164 .ia_atime
= stat
->atime
,
165 .ia_mtime
= stat
->mtime
,
168 return notify_change(upperdentry
, &attr
, NULL
);
171 int ovl_set_attr(struct dentry
*upperdentry
, struct kstat
*stat
)
175 if (!S_ISLNK(stat
->mode
)) {
176 struct iattr attr
= {
177 .ia_valid
= ATTR_MODE
,
178 .ia_mode
= stat
->mode
,
180 err
= notify_change(upperdentry
, &attr
, NULL
);
183 struct iattr attr
= {
184 .ia_valid
= ATTR_UID
| ATTR_GID
,
188 err
= notify_change(upperdentry
, &attr
, NULL
);
191 ovl_set_timestamps(upperdentry
, stat
);
196 static int ovl_copy_up_locked(struct dentry
*workdir
, struct dentry
*upperdir
,
197 struct dentry
*dentry
, struct path
*lowerpath
,
198 struct kstat
*stat
, const char *link
)
200 struct inode
*wdir
= workdir
->d_inode
;
201 struct inode
*udir
= upperdir
->d_inode
;
202 struct dentry
*newdentry
= NULL
;
203 struct dentry
*upper
= NULL
;
204 umode_t mode
= stat
->mode
;
207 newdentry
= ovl_lookup_temp(workdir
, dentry
);
208 err
= PTR_ERR(newdentry
);
209 if (IS_ERR(newdentry
))
212 upper
= lookup_one_len(dentry
->d_name
.name
, upperdir
,
214 err
= PTR_ERR(upper
);
218 /* Can't properly set mode on creation because of the umask */
219 stat
->mode
&= S_IFMT
;
220 err
= ovl_create_real(wdir
, newdentry
, stat
, link
, NULL
, true);
225 if (S_ISREG(stat
->mode
)) {
226 struct path upperpath
;
227 ovl_path_upper(dentry
, &upperpath
);
228 BUG_ON(upperpath
.dentry
!= NULL
);
229 upperpath
.dentry
= newdentry
;
231 err
= ovl_copy_up_data(lowerpath
, &upperpath
, stat
->size
);
236 err
= ovl_copy_xattr(lowerpath
->dentry
, newdentry
);
240 mutex_lock(&newdentry
->d_inode
->i_mutex
);
241 err
= ovl_set_attr(newdentry
, stat
);
242 mutex_unlock(&newdentry
->d_inode
->i_mutex
);
246 err
= ovl_do_rename(wdir
, newdentry
, udir
, upper
, 0);
250 ovl_dentry_update(dentry
, newdentry
);
254 * Non-directores become opaque when copied up.
256 if (!S_ISDIR(stat
->mode
))
257 ovl_dentry_set_opaque(dentry
, true);
266 ovl_cleanup(wdir
, newdentry
);
271 * Copy up a single dentry
273 * Directory renames only allowed on "pure upper" (already created on
274 * upper filesystem, never copied up). Directories which are on lower or
275 * are merged may not be renamed. For these -EXDEV is returned and
276 * userspace has to deal with it. This means, when copying up a
277 * directory we can rely on it and ancestors being stable.
279 * Non-directory renames start with copy up of source if necessary. The
280 * actual rename will only proceed once the copy up was successful. Copy
281 * up uses upper parent i_mutex for exclusion. Since rename can change
282 * d_parent it is possible that the copy up will lock the old parent. At
283 * that point the file will have already been copied up anyway.
285 int ovl_copy_up_one(struct dentry
*parent
, struct dentry
*dentry
,
286 struct path
*lowerpath
, struct kstat
*stat
)
288 struct dentry
*workdir
= ovl_workdir(dentry
);
291 struct path parentpath
;
292 struct dentry
*upperdir
;
293 struct dentry
*upperdentry
;
294 const struct cred
*old_cred
;
295 struct cred
*override_cred
;
298 if (WARN_ON(!workdir
))
301 ovl_path_upper(parent
, &parentpath
);
302 upperdir
= parentpath
.dentry
;
304 err
= vfs_getattr(&parentpath
, &pstat
);
308 if (S_ISLNK(stat
->mode
)) {
309 link
= ovl_read_symlink(lowerpath
->dentry
);
311 return PTR_ERR(link
);
315 override_cred
= prepare_creds();
319 override_cred
->fsuid
= stat
->uid
;
320 override_cred
->fsgid
= stat
->gid
;
322 * CAP_SYS_ADMIN for copying up extended attributes
323 * CAP_DAC_OVERRIDE for create
324 * CAP_FOWNER for chmod, timestamp update
325 * CAP_FSETID for chmod
326 * CAP_CHOWN for chown
327 * CAP_MKNOD for mknod
329 cap_raise(override_cred
->cap_effective
, CAP_SYS_ADMIN
);
330 cap_raise(override_cred
->cap_effective
, CAP_DAC_OVERRIDE
);
331 cap_raise(override_cred
->cap_effective
, CAP_FOWNER
);
332 cap_raise(override_cred
->cap_effective
, CAP_FSETID
);
333 cap_raise(override_cred
->cap_effective
, CAP_CHOWN
);
334 cap_raise(override_cred
->cap_effective
, CAP_MKNOD
);
335 old_cred
= override_creds(override_cred
);
338 if (lock_rename(workdir
, upperdir
) != NULL
) {
339 pr_err("overlayfs: failed to lock workdir+upperdir\n");
342 upperdentry
= ovl_dentry_upper(dentry
);
344 /* Raced with another copy-up? Nothing to do, then... */
349 err
= ovl_copy_up_locked(workdir
, upperdir
, dentry
, lowerpath
,
352 /* Restore timestamps on parent (best effort) */
353 ovl_set_timestamps(upperdir
, &pstat
);
356 unlock_rename(workdir
, upperdir
);
357 revert_creds(old_cred
);
358 put_cred(override_cred
);
362 free_page((unsigned long) link
);
367 int ovl_copy_up(struct dentry
*dentry
)
374 struct dentry
*parent
;
375 struct path lowerpath
;
377 enum ovl_path_type type
= ovl_path_type(dentry
);
379 if (OVL_TYPE_UPPER(type
))
383 /* find the topmost dentry not yet copied up */
385 parent
= dget_parent(next
);
387 type
= ovl_path_type(parent
);
388 if (OVL_TYPE_UPPER(type
))
395 ovl_path_lower(next
, &lowerpath
);
396 err
= vfs_getattr(&lowerpath
, &stat
);
398 err
= ovl_copy_up_one(parent
, next
, &lowerpath
, &stat
);