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/signal.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"
24 #include "ovl_entry.h"
26 #define OVL_COPY_UP_CHUNK_SIZE (1 << 20)
28 static bool __read_mostly ovl_check_copy_up
;
29 module_param_named(check_copy_up
, ovl_check_copy_up
, bool,
31 MODULE_PARM_DESC(ovl_check_copy_up
,
32 "Warn on copy-up when causing process also has a R/O fd open");
34 static int ovl_check_fd(const void *data
, struct file
*f
, unsigned int fd
)
36 const struct dentry
*dentry
= data
;
38 if (file_inode(f
) == d_inode(dentry
))
39 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",
40 f
, fd
, current
->pid
, current
->comm
);
45 * Check the fds open by this process and warn if something like the following
46 * scenario is about to occur:
48 * fd1 = open("foo", O_RDONLY);
49 * fd2 = open("foo", O_RDWR);
51 static void ovl_do_check_copy_up(struct dentry
*dentry
)
53 if (ovl_check_copy_up
)
54 iterate_fd(current
->files
, 0, ovl_check_fd
, dentry
);
57 int ovl_copy_xattr(struct dentry
*old
, struct dentry
*new)
59 ssize_t list_size
, size
, value_size
= 0;
60 char *buf
, *name
, *value
= NULL
;
61 int uninitialized_var(error
);
64 if (!(old
->d_inode
->i_opflags
& IOP_XATTR
) ||
65 !(new->d_inode
->i_opflags
& IOP_XATTR
))
68 list_size
= vfs_listxattr(old
, NULL
, 0);
70 if (list_size
== -EOPNOTSUPP
)
75 buf
= kzalloc(list_size
, GFP_KERNEL
);
79 list_size
= vfs_listxattr(old
, buf
, list_size
);
85 for (name
= buf
; list_size
; name
+= slen
) {
86 slen
= strnlen(name
, list_size
) + 1;
88 /* underlying fs providing us with an broken xattr list? */
89 if (WARN_ON(slen
> list_size
)) {
95 if (ovl_is_private_xattr(name
))
98 size
= vfs_getxattr(old
, name
, value
, value_size
);
100 size
= vfs_getxattr(old
, name
, NULL
, 0);
107 if (size
> value_size
) {
110 new = krealloc(value
, size
, GFP_KERNEL
);
120 error
= security_inode_copy_up_xattr(name
);
121 if (error
< 0 && error
!= -EOPNOTSUPP
)
125 continue; /* Discard */
127 error
= vfs_setxattr(new, name
, value
, size
, 0);
137 static int ovl_copy_up_data(struct path
*old
, struct path
*new, loff_t len
)
139 struct file
*old_file
;
140 struct file
*new_file
;
148 old_file
= ovl_path_open(old
, O_LARGEFILE
| O_RDONLY
);
149 if (IS_ERR(old_file
))
150 return PTR_ERR(old_file
);
152 new_file
= ovl_path_open(new, O_LARGEFILE
| O_WRONLY
);
153 if (IS_ERR(new_file
)) {
154 error
= PTR_ERR(new_file
);
158 /* Try to use clone_file_range to clone up within the same fs */
159 error
= vfs_clone_file_range(old_file
, 0, new_file
, 0, len
);
162 /* Couldn't clone, so now we try to copy the data */
165 /* FIXME: copy up sparse files efficiently */
167 size_t this_len
= OVL_COPY_UP_CHUNK_SIZE
;
173 if (signal_pending_state(TASK_KILLABLE
, current
)) {
178 bytes
= do_splice_direct(old_file
, &old_pos
,
180 this_len
, SPLICE_F_MOVE
);
185 WARN_ON(old_pos
!= new_pos
);
191 error
= vfs_fsync(new_file
, 0);
198 static int ovl_set_timestamps(struct dentry
*upperdentry
, struct kstat
*stat
)
200 struct iattr attr
= {
202 ATTR_ATIME
| ATTR_MTIME
| ATTR_ATIME_SET
| ATTR_MTIME_SET
,
203 .ia_atime
= stat
->atime
,
204 .ia_mtime
= stat
->mtime
,
207 return notify_change(upperdentry
, &attr
, NULL
);
210 int ovl_set_attr(struct dentry
*upperdentry
, struct kstat
*stat
)
214 if (!S_ISLNK(stat
->mode
)) {
215 struct iattr attr
= {
216 .ia_valid
= ATTR_MODE
,
217 .ia_mode
= stat
->mode
,
219 err
= notify_change(upperdentry
, &attr
, NULL
);
222 struct iattr attr
= {
223 .ia_valid
= ATTR_UID
| ATTR_GID
,
227 err
= notify_change(upperdentry
, &attr
, NULL
);
230 ovl_set_timestamps(upperdentry
, stat
);
235 static int ovl_copy_up_locked(struct dentry
*workdir
, struct dentry
*upperdir
,
236 struct dentry
*dentry
, struct path
*lowerpath
,
237 struct kstat
*stat
, const char *link
,
238 struct kstat
*pstat
, bool tmpfile
)
240 struct inode
*wdir
= workdir
->d_inode
;
241 struct inode
*udir
= upperdir
->d_inode
;
242 struct dentry
*newdentry
= NULL
;
243 struct dentry
*upper
= NULL
;
244 struct dentry
*temp
= NULL
;
246 const struct cred
*old_creds
= NULL
;
247 struct cred
*new_creds
= NULL
;
248 struct cattr cattr
= {
249 /* Can't properly set mode on creation because of the umask */
250 .mode
= stat
->mode
& S_IFMT
,
255 err
= security_inode_copy_up(dentry
, &new_creds
);
260 old_creds
= override_creds(new_creds
);
263 temp
= ovl_do_tmpfile(upperdir
, stat
->mode
);
265 temp
= ovl_lookup_temp(workdir
, dentry
);
272 if (!err
&& !tmpfile
)
273 err
= ovl_create_real(wdir
, temp
, &cattr
, NULL
, true);
276 revert_creds(old_creds
);
283 if (S_ISREG(stat
->mode
)) {
284 struct path upperpath
;
286 ovl_path_upper(dentry
, &upperpath
);
287 BUG_ON(upperpath
.dentry
!= NULL
);
288 upperpath
.dentry
= temp
;
292 err
= ovl_copy_up_data(lowerpath
, &upperpath
,
294 inode_lock_nested(udir
, I_MUTEX_PARENT
);
296 err
= ovl_copy_up_data(lowerpath
, &upperpath
,
304 err
= ovl_copy_xattr(lowerpath
->dentry
, temp
);
308 inode_lock(temp
->d_inode
);
309 err
= ovl_set_attr(temp
, stat
);
310 inode_unlock(temp
->d_inode
);
314 upper
= lookup_one_len(dentry
->d_name
.name
, upperdir
,
317 err
= PTR_ERR(upper
);
323 err
= ovl_do_link(temp
, udir
, upper
, true);
325 err
= ovl_do_rename(wdir
, temp
, udir
, upper
, 0);
329 newdentry
= dget(tmpfile
? upper
: temp
);
330 ovl_dentry_update(dentry
, newdentry
);
331 ovl_inode_update(d_inode(dentry
), d_inode(newdentry
));
333 /* Restore timestamps on parent (best effort) */
334 ovl_set_timestamps(upperdir
, pstat
);
342 ovl_cleanup(wdir
, temp
);
347 * Copy up a single dentry
349 * All renames start with copy up of source if necessary. The actual
350 * rename will only proceed once the copy up was successful. Copy up uses
351 * upper parent i_mutex for exclusion. Since rename can change d_parent it
352 * is possible that the copy up will lock the old parent. At that point
353 * the file will have already been copied up anyway.
355 static int ovl_copy_up_one(struct dentry
*parent
, struct dentry
*dentry
,
356 struct path
*lowerpath
, struct kstat
*stat
)
358 DEFINE_DELAYED_CALL(done
);
359 struct dentry
*workdir
= ovl_workdir(dentry
);
362 struct path parentpath
;
363 struct dentry
*lowerdentry
= lowerpath
->dentry
;
364 struct dentry
*upperdir
;
365 const char *link
= NULL
;
366 struct ovl_fs
*ofs
= dentry
->d_sb
->s_fs_info
;
368 if (WARN_ON(!workdir
))
371 ovl_do_check_copy_up(lowerdentry
);
373 ovl_path_upper(parent
, &parentpath
);
374 upperdir
= parentpath
.dentry
;
376 err
= vfs_getattr(&parentpath
, &pstat
,
377 STATX_ATIME
| STATX_MTIME
, AT_STATX_SYNC_AS_STAT
);
381 if (S_ISLNK(stat
->mode
)) {
382 link
= vfs_get_link(lowerdentry
, &done
);
384 return PTR_ERR(link
);
387 /* Should we copyup with O_TMPFILE or with workdir? */
388 if (S_ISREG(stat
->mode
) && ofs
->tmpfile
) {
389 err
= ovl_copy_up_start(dentry
);
390 /* err < 0: interrupted, err > 0: raced with another copy-up */
392 pr_debug("ovl_copy_up_start(%pd2) = %i\n", dentry
, err
);
398 inode_lock_nested(upperdir
->d_inode
, I_MUTEX_PARENT
);
399 err
= ovl_copy_up_locked(workdir
, upperdir
, dentry
, lowerpath
,
400 stat
, link
, &pstat
, true);
401 inode_unlock(upperdir
->d_inode
);
402 ovl_copy_up_end(dentry
);
407 if (lock_rename(workdir
, upperdir
) != NULL
) {
408 pr_err("overlayfs: failed to lock workdir+upperdir\n");
411 if (ovl_dentry_upper(dentry
)) {
412 /* Raced with another copy-up? Nothing to do, then... */
417 err
= ovl_copy_up_locked(workdir
, upperdir
, dentry
, lowerpath
,
418 stat
, link
, &pstat
, false);
420 unlock_rename(workdir
, upperdir
);
422 do_delayed_call(&done
);
427 int ovl_copy_up_flags(struct dentry
*dentry
, int flags
)
430 const struct cred
*old_cred
= ovl_override_creds(dentry
->d_sb
);
434 struct dentry
*parent
;
435 struct path lowerpath
;
437 enum ovl_path_type type
= ovl_path_type(dentry
);
439 if (OVL_TYPE_UPPER(type
))
443 /* find the topmost dentry not yet copied up */
445 parent
= dget_parent(next
);
447 type
= ovl_path_type(parent
);
448 if (OVL_TYPE_UPPER(type
))
455 ovl_path_lower(next
, &lowerpath
);
456 err
= vfs_getattr(&lowerpath
, &stat
,
457 STATX_BASIC_STATS
, AT_STATX_SYNC_AS_STAT
);
458 /* maybe truncate regular file. this has no effect on dirs */
462 err
= ovl_copy_up_one(parent
, next
, &lowerpath
, &stat
);
467 revert_creds(old_cred
);
472 int ovl_copy_up(struct dentry
*dentry
)
474 return ovl_copy_up_flags(dentry
, 0);