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 <linux/exportfs.h>
24 #include "overlayfs.h"
25 #include "ovl_entry.h"
27 #define OVL_COPY_UP_CHUNK_SIZE (1 << 20)
29 static bool __read_mostly ovl_check_copy_up
;
30 module_param_named(check_copy_up
, ovl_check_copy_up
, bool,
32 MODULE_PARM_DESC(ovl_check_copy_up
,
33 "Warn on copy-up when causing process also has a R/O fd open");
35 static int ovl_check_fd(const void *data
, struct file
*f
, unsigned int fd
)
37 const struct dentry
*dentry
= data
;
39 if (file_inode(f
) == d_inode(dentry
))
40 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",
41 f
, fd
, current
->pid
, current
->comm
);
46 * Check the fds open by this process and warn if something like the following
47 * scenario is about to occur:
49 * fd1 = open("foo", O_RDONLY);
50 * fd2 = open("foo", O_RDWR);
52 static void ovl_do_check_copy_up(struct dentry
*dentry
)
54 if (ovl_check_copy_up
)
55 iterate_fd(current
->files
, 0, ovl_check_fd
, dentry
);
58 int ovl_copy_xattr(struct dentry
*old
, struct dentry
*new)
60 ssize_t list_size
, size
, value_size
= 0;
61 char *buf
, *name
, *value
= NULL
;
62 int uninitialized_var(error
);
65 if (!(old
->d_inode
->i_opflags
& IOP_XATTR
) ||
66 !(new->d_inode
->i_opflags
& IOP_XATTR
))
69 list_size
= vfs_listxattr(old
, NULL
, 0);
71 if (list_size
== -EOPNOTSUPP
)
76 buf
= kzalloc(list_size
, GFP_KERNEL
);
80 list_size
= vfs_listxattr(old
, buf
, list_size
);
86 for (name
= buf
; list_size
; name
+= slen
) {
87 slen
= strnlen(name
, list_size
) + 1;
89 /* underlying fs providing us with an broken xattr list? */
90 if (WARN_ON(slen
> list_size
)) {
96 if (ovl_is_private_xattr(name
))
99 size
= vfs_getxattr(old
, name
, value
, value_size
);
101 size
= vfs_getxattr(old
, name
, NULL
, 0);
108 if (size
> value_size
) {
111 new = krealloc(value
, size
, GFP_KERNEL
);
121 error
= security_inode_copy_up_xattr(name
);
122 if (error
< 0 && error
!= -EOPNOTSUPP
)
126 continue; /* Discard */
128 error
= vfs_setxattr(new, name
, value
, size
, 0);
138 static int ovl_copy_up_data(struct path
*old
, struct path
*new, loff_t len
)
140 struct file
*old_file
;
141 struct file
*new_file
;
149 old_file
= ovl_path_open(old
, O_LARGEFILE
| O_RDONLY
);
150 if (IS_ERR(old_file
))
151 return PTR_ERR(old_file
);
153 new_file
= ovl_path_open(new, O_LARGEFILE
| O_WRONLY
);
154 if (IS_ERR(new_file
)) {
155 error
= PTR_ERR(new_file
);
159 /* Try to use clone_file_range to clone up within the same fs */
160 error
= vfs_clone_file_range(old_file
, 0, new_file
, 0, len
);
163 /* Couldn't clone, so now we try to copy the data */
166 /* FIXME: copy up sparse files efficiently */
168 size_t this_len
= OVL_COPY_UP_CHUNK_SIZE
;
174 if (signal_pending_state(TASK_KILLABLE
, current
)) {
179 bytes
= do_splice_direct(old_file
, &old_pos
,
181 this_len
, SPLICE_F_MOVE
);
186 WARN_ON(old_pos
!= new_pos
);
192 error
= vfs_fsync(new_file
, 0);
199 static int ovl_set_timestamps(struct dentry
*upperdentry
, struct kstat
*stat
)
201 struct iattr attr
= {
203 ATTR_ATIME
| ATTR_MTIME
| ATTR_ATIME_SET
| ATTR_MTIME_SET
,
204 .ia_atime
= stat
->atime
,
205 .ia_mtime
= stat
->mtime
,
208 return notify_change(upperdentry
, &attr
, NULL
);
211 int ovl_set_attr(struct dentry
*upperdentry
, struct kstat
*stat
)
215 if (!S_ISLNK(stat
->mode
)) {
216 struct iattr attr
= {
217 .ia_valid
= ATTR_MODE
,
218 .ia_mode
= stat
->mode
,
220 err
= notify_change(upperdentry
, &attr
, NULL
);
223 struct iattr attr
= {
224 .ia_valid
= ATTR_UID
| ATTR_GID
,
228 err
= notify_change(upperdentry
, &attr
, NULL
);
231 ovl_set_timestamps(upperdentry
, stat
);
236 struct ovl_fh
*ovl_encode_fh(struct dentry
*lower
, bool is_upper
)
239 int fh_type
, fh_len
, dwords
;
241 int buflen
= MAX_HANDLE_SZ
;
242 uuid_t
*uuid
= &lower
->d_sb
->s_uuid
;
244 buf
= kmalloc(buflen
, GFP_TEMPORARY
);
246 return ERR_PTR(-ENOMEM
);
249 * We encode a non-connectable file handle for non-dir, because we
250 * only need to find the lower inode number and we don't want to pay
251 * the price or reconnecting the dentry.
253 dwords
= buflen
>> 2;
254 fh_type
= exportfs_encode_fh(lower
, buf
, &dwords
, 0);
255 buflen
= (dwords
<< 2);
258 if (WARN_ON(fh_type
< 0) ||
259 WARN_ON(buflen
> MAX_HANDLE_SZ
) ||
260 WARN_ON(fh_type
== FILEID_INVALID
))
263 BUILD_BUG_ON(MAX_HANDLE_SZ
+ offsetof(struct ovl_fh
, fid
) > 255);
264 fh_len
= offsetof(struct ovl_fh
, fid
) + buflen
;
265 fh
= kmalloc(fh_len
, GFP_KERNEL
);
267 fh
= ERR_PTR(-ENOMEM
);
271 fh
->version
= OVL_FH_VERSION
;
272 fh
->magic
= OVL_FH_MAGIC
;
274 fh
->flags
= OVL_FH_FLAG_CPU_ENDIAN
;
276 * When we will want to decode an overlay dentry from this handle
277 * and all layers are on the same fs, if we get a disconncted real
278 * dentry when we decode fid, the only way to tell if we should assign
279 * it to upperdentry or to lowerstack is by checking this flag.
282 fh
->flags
|= OVL_FH_FLAG_PATH_UPPER
;
285 memcpy(fh
->fid
, buf
, buflen
);
292 static int ovl_set_origin(struct dentry
*dentry
, struct dentry
*lower
,
293 struct dentry
*upper
)
295 const struct ovl_fh
*fh
= NULL
;
299 * When lower layer doesn't support export operations store a 'null' fh,
300 * so we can use the overlay.origin xattr to distignuish between a copy
301 * up and a pure upper inode.
303 if (ovl_can_decode_fh(lower
->d_sb
)) {
304 fh
= ovl_encode_fh(lower
, false);
310 * Do not fail when upper doesn't support xattrs.
312 err
= ovl_check_setxattr(dentry
, upper
, OVL_XATTR_ORIGIN
, fh
,
313 fh
? fh
->len
: 0, 0);
319 struct ovl_copy_up_ctx
{
320 struct dentry
*dentry
;
321 struct path lowerpath
;
325 struct dentry
*upperdir
;
326 struct dentry
*workdir
;
330 static int ovl_install_temp(struct ovl_copy_up_ctx
*c
, struct dentry
*temp
,
331 struct dentry
**newdentry
)
334 struct dentry
*upper
;
335 struct inode
*udir
= d_inode(c
->upperdir
);
337 upper
= lookup_one_len(c
->dentry
->d_name
.name
, c
->upperdir
,
338 c
->dentry
->d_name
.len
);
340 return PTR_ERR(upper
);
343 err
= ovl_do_link(temp
, udir
, upper
, true);
345 err
= ovl_do_rename(d_inode(c
->workdir
), temp
, udir
, upper
, 0);
347 /* Restore timestamps on parent (best effort) */
349 ovl_set_timestamps(c
->upperdir
, &c
->pstat
);
350 *newdentry
= dget(c
->tmpfile
? upper
: temp
);
357 static int ovl_get_tmpfile(struct ovl_copy_up_ctx
*c
, struct dentry
**tempp
)
361 const struct cred
*old_creds
= NULL
;
362 struct cred
*new_creds
= NULL
;
363 struct cattr cattr
= {
364 /* Can't properly set mode on creation because of the umask */
365 .mode
= c
->stat
.mode
& S_IFMT
,
366 .rdev
= c
->stat
.rdev
,
370 err
= security_inode_copy_up(c
->dentry
, &new_creds
);
375 old_creds
= override_creds(new_creds
);
378 temp
= ovl_do_tmpfile(c
->workdir
, c
->stat
.mode
);
382 temp
= ovl_lookup_temp(c
->workdir
);
386 err
= ovl_create_real(d_inode(c
->workdir
), temp
, &cattr
,
397 revert_creds(old_creds
);
408 static int ovl_copy_up_inode(struct ovl_copy_up_ctx
*c
, struct dentry
*temp
)
412 if (S_ISREG(c
->stat
.mode
)) {
413 struct path upperpath
;
415 ovl_path_upper(c
->dentry
, &upperpath
);
416 BUG_ON(upperpath
.dentry
!= NULL
);
417 upperpath
.dentry
= temp
;
419 err
= ovl_copy_up_data(&c
->lowerpath
, &upperpath
, c
->stat
.size
);
424 err
= ovl_copy_xattr(c
->lowerpath
.dentry
, temp
);
428 inode_lock(temp
->d_inode
);
429 err
= ovl_set_attr(temp
, &c
->stat
);
430 inode_unlock(temp
->d_inode
);
435 * Store identifier of lower inode in upper inode xattr to
436 * allow lookup of the copy up origin inode.
438 * Don't set origin when we are breaking the association with a lower
441 if (S_ISDIR(c
->stat
.mode
) || c
->stat
.nlink
== 1) {
442 err
= ovl_set_origin(c
->dentry
, c
->lowerpath
.dentry
, temp
);
450 static int ovl_copy_up_locked(struct ovl_copy_up_ctx
*c
)
452 struct inode
*udir
= c
->upperdir
->d_inode
;
453 struct dentry
*newdentry
= NULL
;
454 struct dentry
*temp
= NULL
;
457 err
= ovl_get_tmpfile(c
, &temp
);
461 err
= ovl_copy_up_inode(c
, temp
);
466 inode_lock_nested(udir
, I_MUTEX_PARENT
);
467 err
= ovl_install_temp(c
, temp
, &newdentry
);
470 err
= ovl_install_temp(c
, temp
, &newdentry
);
475 ovl_inode_update(d_inode(c
->dentry
), newdentry
);
482 ovl_cleanup(d_inode(c
->workdir
), temp
);
487 * Copy up a single dentry
489 * All renames start with copy up of source if necessary. The actual
490 * rename will only proceed once the copy up was successful. Copy up uses
491 * upper parent i_mutex for exclusion. Since rename can change d_parent it
492 * is possible that the copy up will lock the old parent. At that point
493 * the file will have already been copied up anyway.
495 static int ovl_copy_up_one(struct dentry
*parent
, struct ovl_copy_up_ctx
*c
)
497 DEFINE_DELAYED_CALL(done
);
499 struct path parentpath
;
500 struct dentry
*lowerdentry
= c
->lowerpath
.dentry
;
501 struct ovl_fs
*ofs
= c
->dentry
->d_sb
->s_fs_info
;
503 c
->workdir
= ovl_workdir(c
->dentry
);
504 if (WARN_ON(!c
->workdir
))
507 ovl_do_check_copy_up(lowerdentry
);
509 ovl_path_upper(parent
, &parentpath
);
510 c
->upperdir
= parentpath
.dentry
;
512 /* Mark parent "impure" because it may now contain non-pure upper */
513 err
= ovl_set_impure(parent
, c
->upperdir
);
517 err
= vfs_getattr(&parentpath
, &c
->pstat
,
518 STATX_ATIME
| STATX_MTIME
, AT_STATX_SYNC_AS_STAT
);
522 if (S_ISLNK(c
->stat
.mode
)) {
523 c
->link
= vfs_get_link(lowerdentry
, &done
);
525 return PTR_ERR(c
->link
);
528 /* Should we copyup with O_TMPFILE or with workdir? */
529 if (S_ISREG(c
->stat
.mode
) && ofs
->tmpfile
) {
530 err
= ovl_copy_up_start(c
->dentry
);
531 /* err < 0: interrupted, err > 0: raced with another copy-up */
533 pr_debug("ovl_copy_up_start(%pd2) = %i\n", c
->dentry
,
540 err
= ovl_copy_up_locked(c
);
541 ovl_copy_up_end(c
->dentry
);
546 if (lock_rename(c
->workdir
, c
->upperdir
) != NULL
) {
547 pr_err("overlayfs: failed to lock workdir+upperdir\n");
550 if (ovl_dentry_upper(c
->dentry
)) {
551 /* Raced with another copy-up? Nothing to do, then... */
556 err
= ovl_copy_up_locked(c
);
558 unlock_rename(c
->workdir
, c
->upperdir
);
560 do_delayed_call(&done
);
565 int ovl_copy_up_flags(struct dentry
*dentry
, int flags
)
568 const struct cred
*old_cred
= ovl_override_creds(dentry
->d_sb
);
572 struct dentry
*parent
;
573 struct ovl_copy_up_ctx ctx
= { };
574 enum ovl_path_type type
= ovl_path_type(dentry
);
576 if (OVL_TYPE_UPPER(type
))
580 /* find the topmost dentry not yet copied up */
582 parent
= dget_parent(next
);
584 type
= ovl_path_type(parent
);
585 if (OVL_TYPE_UPPER(type
))
592 ovl_path_lower(next
, &ctx
.lowerpath
);
593 err
= vfs_getattr(&ctx
.lowerpath
, &ctx
.stat
,
594 STATX_BASIC_STATS
, AT_STATX_SYNC_AS_STAT
);
595 /* maybe truncate regular file. this has no effect on dirs */
600 err
= ovl_copy_up_one(parent
, &ctx
);
606 revert_creds(old_cred
);
611 int ovl_copy_up(struct dentry
*dentry
)
613 return ovl_copy_up_flags(dentry
, 0);