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
);
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
);
44 list_size
= vfs_listxattr(old
, buf
, list_size
);
50 for (name
= buf
; name
< (buf
+ list_size
); name
+= strlen(name
) + 1) {
52 size
= vfs_getxattr(old
, name
, value
, value_size
);
54 size
= vfs_getxattr(old
, name
, NULL
, 0);
61 if (size
> value_size
) {
64 new = krealloc(value
, size
, GFP_KERNEL
);
74 error
= vfs_setxattr(new, name
, value
, size
, 0);
84 static int ovl_copy_up_data(struct path
*old
, struct path
*new, loff_t len
)
86 struct file
*old_file
;
87 struct file
*new_file
;
95 old_file
= ovl_path_open(old
, O_LARGEFILE
| O_RDONLY
);
97 return PTR_ERR(old_file
);
99 new_file
= ovl_path_open(new, O_LARGEFILE
| O_WRONLY
);
100 if (IS_ERR(new_file
)) {
101 error
= PTR_ERR(new_file
);
105 /* FIXME: copy up sparse files efficiently */
107 size_t this_len
= OVL_COPY_UP_CHUNK_SIZE
;
113 if (signal_pending_state(TASK_KILLABLE
, current
)) {
118 bytes
= do_splice_direct(old_file
, &old_pos
,
120 this_len
, SPLICE_F_MOVE
);
125 WARN_ON(old_pos
!= new_pos
);
136 static char *ovl_read_symlink(struct dentry
*realdentry
)
140 struct inode
*inode
= realdentry
->d_inode
;
144 if (!inode
->i_op
->readlink
)
148 buf
= (char *) __get_free_page(GFP_KERNEL
);
154 /* The cast to a user pointer is valid due to the set_fs() */
155 res
= inode
->i_op
->readlink(realdentry
,
156 (char __user
*)buf
, PAGE_SIZE
- 1);
159 free_page((unsigned long) buf
);
170 static int ovl_set_timestamps(struct dentry
*upperdentry
, struct kstat
*stat
)
172 struct iattr attr
= {
174 ATTR_ATIME
| ATTR_MTIME
| ATTR_ATIME_SET
| ATTR_MTIME_SET
,
175 .ia_atime
= stat
->atime
,
176 .ia_mtime
= stat
->mtime
,
179 return notify_change(upperdentry
, &attr
, NULL
);
182 int ovl_set_attr(struct dentry
*upperdentry
, struct kstat
*stat
)
186 if (!S_ISLNK(stat
->mode
)) {
187 struct iattr attr
= {
188 .ia_valid
= ATTR_MODE
,
189 .ia_mode
= stat
->mode
,
191 err
= notify_change(upperdentry
, &attr
, NULL
);
194 struct iattr attr
= {
195 .ia_valid
= ATTR_UID
| ATTR_GID
,
199 err
= notify_change(upperdentry
, &attr
, NULL
);
202 ovl_set_timestamps(upperdentry
, stat
);
207 static int ovl_copy_up_locked(struct dentry
*workdir
, struct dentry
*upperdir
,
208 struct dentry
*dentry
, struct path
*lowerpath
,
209 struct kstat
*stat
, const char *link
)
211 struct inode
*wdir
= workdir
->d_inode
;
212 struct inode
*udir
= upperdir
->d_inode
;
213 struct dentry
*newdentry
= NULL
;
214 struct dentry
*upper
= NULL
;
215 umode_t mode
= stat
->mode
;
218 newdentry
= ovl_lookup_temp(workdir
, dentry
);
219 err
= PTR_ERR(newdentry
);
220 if (IS_ERR(newdentry
))
223 upper
= lookup_one_len(dentry
->d_name
.name
, upperdir
,
225 err
= PTR_ERR(upper
);
229 /* Can't properly set mode on creation because of the umask */
230 stat
->mode
&= S_IFMT
;
231 err
= ovl_create_real(wdir
, newdentry
, stat
, link
, NULL
, true);
236 if (S_ISREG(stat
->mode
)) {
237 struct path upperpath
;
238 ovl_path_upper(dentry
, &upperpath
);
239 BUG_ON(upperpath
.dentry
!= NULL
);
240 upperpath
.dentry
= newdentry
;
242 err
= ovl_copy_up_data(lowerpath
, &upperpath
, stat
->size
);
247 err
= ovl_copy_xattr(lowerpath
->dentry
, newdentry
);
251 inode_lock(newdentry
->d_inode
);
252 err
= ovl_set_attr(newdentry
, stat
);
253 inode_unlock(newdentry
->d_inode
);
257 err
= ovl_do_rename(wdir
, newdentry
, udir
, upper
, 0);
261 ovl_dentry_update(dentry
, newdentry
);
265 * Non-directores become opaque when copied up.
267 if (!S_ISDIR(stat
->mode
))
268 ovl_dentry_set_opaque(dentry
, true);
277 ovl_cleanup(wdir
, newdentry
);
282 * Copy up a single dentry
284 * Directory renames only allowed on "pure upper" (already created on
285 * upper filesystem, never copied up). Directories which are on lower or
286 * are merged may not be renamed. For these -EXDEV is returned and
287 * userspace has to deal with it. This means, when copying up a
288 * directory we can rely on it and ancestors being stable.
290 * Non-directory renames start with copy up of source if necessary. The
291 * actual rename will only proceed once the copy up was successful. Copy
292 * up uses upper parent i_mutex for exclusion. Since rename can change
293 * d_parent it is possible that the copy up will lock the old parent. At
294 * that point the file will have already been copied up anyway.
296 int ovl_copy_up_one(struct dentry
*parent
, struct dentry
*dentry
,
297 struct path
*lowerpath
, struct kstat
*stat
)
299 struct dentry
*workdir
= ovl_workdir(dentry
);
302 struct path parentpath
;
303 struct dentry
*upperdir
;
304 struct dentry
*upperdentry
;
305 const struct cred
*old_cred
;
306 struct cred
*override_cred
;
309 if (WARN_ON(!workdir
))
312 ovl_path_upper(parent
, &parentpath
);
313 upperdir
= parentpath
.dentry
;
315 err
= vfs_getattr(&parentpath
, &pstat
);
319 if (S_ISLNK(stat
->mode
)) {
320 link
= ovl_read_symlink(lowerpath
->dentry
);
322 return PTR_ERR(link
);
326 override_cred
= prepare_creds();
330 override_cred
->fsuid
= stat
->uid
;
331 override_cred
->fsgid
= stat
->gid
;
333 * CAP_SYS_ADMIN for copying up extended attributes
334 * CAP_DAC_OVERRIDE for create
335 * CAP_FOWNER for chmod, timestamp update
336 * CAP_FSETID for chmod
337 * CAP_CHOWN for chown
338 * CAP_MKNOD for mknod
340 cap_raise(override_cred
->cap_effective
, CAP_SYS_ADMIN
);
341 cap_raise(override_cred
->cap_effective
, CAP_DAC_OVERRIDE
);
342 cap_raise(override_cred
->cap_effective
, CAP_FOWNER
);
343 cap_raise(override_cred
->cap_effective
, CAP_FSETID
);
344 cap_raise(override_cred
->cap_effective
, CAP_CHOWN
);
345 cap_raise(override_cred
->cap_effective
, CAP_MKNOD
);
346 old_cred
= override_creds(override_cred
);
349 if (lock_rename(workdir
, upperdir
) != NULL
) {
350 pr_err("overlayfs: failed to lock workdir+upperdir\n");
353 upperdentry
= ovl_dentry_upper(dentry
);
355 /* Raced with another copy-up? Nothing to do, then... */
360 err
= ovl_copy_up_locked(workdir
, upperdir
, dentry
, lowerpath
,
363 /* Restore timestamps on parent (best effort) */
364 ovl_set_timestamps(upperdir
, &pstat
);
367 unlock_rename(workdir
, upperdir
);
368 revert_creds(old_cred
);
369 put_cred(override_cred
);
373 free_page((unsigned long) link
);
378 int ovl_copy_up(struct dentry
*dentry
)
385 struct dentry
*parent
;
386 struct path lowerpath
;
388 enum ovl_path_type type
= ovl_path_type(dentry
);
390 if (OVL_TYPE_UPPER(type
))
394 /* find the topmost dentry not yet copied up */
396 parent
= dget_parent(next
);
398 type
= ovl_path_type(parent
);
399 if (OVL_TYPE_UPPER(type
))
406 ovl_path_lower(next
, &lowerpath
);
407 err
= vfs_getattr(&lowerpath
, &stat
);
409 err
= ovl_copy_up_one(parent
, next
, &lowerpath
, &stat
);