]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/overlayfs/copy_up.c
sched/headers: Prepare header dependency changes, move the <asm/paravirt.h> include...
[mirror_ubuntu-bionic-kernel.git] / fs / overlayfs / copy_up.c
CommitLineData
e9be9d5e
MS
1/*
2 *
3 * Copyright (C) 2011 Novell Inc.
4 *
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.
8 */
9
fb5bb2c3 10#include <linux/module.h>
e9be9d5e
MS
11#include <linux/fs.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.h>
19#include <linux/namei.h>
fb5bb2c3
DH
20#include <linux/fdtable.h>
21#include <linux/ratelimit.h>
e9be9d5e
MS
22#include "overlayfs.h"
23
24#define OVL_COPY_UP_CHUNK_SIZE (1 << 20)
25
fb5bb2c3
DH
26static bool __read_mostly ovl_check_copy_up;
27module_param_named(check_copy_up, ovl_check_copy_up, bool,
28 S_IWUSR | S_IRUGO);
29MODULE_PARM_DESC(ovl_check_copy_up,
30 "Warn on copy-up when causing process also has a R/O fd open");
31
32static int ovl_check_fd(const void *data, struct file *f, unsigned int fd)
33{
34 const struct dentry *dentry = data;
35
45063097 36 if (file_inode(f) == d_inode(dentry))
fb5bb2c3
DH
37 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",
38 f, fd, current->pid, current->comm);
39 return 0;
40}
41
42/*
43 * Check the fds open by this process and warn if something like the following
44 * scenario is about to occur:
45 *
46 * fd1 = open("foo", O_RDONLY);
47 * fd2 = open("foo", O_RDWR);
48 */
49static void ovl_do_check_copy_up(struct dentry *dentry)
50{
51 if (ovl_check_copy_up)
52 iterate_fd(current->files, 0, ovl_check_fd, dentry);
53}
54
e9be9d5e
MS
55int ovl_copy_xattr(struct dentry *old, struct dentry *new)
56{
e4ad29fa
VC
57 ssize_t list_size, size, value_size = 0;
58 char *buf, *name, *value = NULL;
59 int uninitialized_var(error);
8b326c61 60 size_t slen;
e9be9d5e 61
5d6c3191
AG
62 if (!(old->d_inode->i_opflags & IOP_XATTR) ||
63 !(new->d_inode->i_opflags & IOP_XATTR))
e9be9d5e
MS
64 return 0;
65
66 list_size = vfs_listxattr(old, NULL, 0);
67 if (list_size <= 0) {
68 if (list_size == -EOPNOTSUPP)
69 return 0;
70 return list_size;
71 }
72
73 buf = kzalloc(list_size, GFP_KERNEL);
74 if (!buf)
75 return -ENOMEM;
76
e9be9d5e
MS
77 list_size = vfs_listxattr(old, buf, list_size);
78 if (list_size <= 0) {
79 error = list_size;
e4ad29fa 80 goto out;
e9be9d5e
MS
81 }
82
8b326c61
MS
83 for (name = buf; list_size; name += slen) {
84 slen = strnlen(name, list_size) + 1;
85
86 /* underlying fs providing us with an broken xattr list? */
87 if (WARN_ON(slen > list_size)) {
88 error = -EIO;
89 break;
90 }
91 list_size -= slen;
92
0956254a
MS
93 if (ovl_is_private_xattr(name))
94 continue;
e4ad29fa
VC
95retry:
96 size = vfs_getxattr(old, name, value, value_size);
97 if (size == -ERANGE)
98 size = vfs_getxattr(old, name, NULL, 0);
99
97daf8b9 100 if (size < 0) {
e9be9d5e 101 error = size;
e4ad29fa 102 break;
e9be9d5e 103 }
e4ad29fa
VC
104
105 if (size > value_size) {
106 void *new;
107
108 new = krealloc(value, size, GFP_KERNEL);
109 if (!new) {
110 error = -ENOMEM;
111 break;
112 }
113 value = new;
114 value_size = size;
115 goto retry;
116 }
117
121ab822
VG
118 error = security_inode_copy_up_xattr(name);
119 if (error < 0 && error != -EOPNOTSUPP)
120 break;
121 if (error == 1) {
122 error = 0;
123 continue; /* Discard */
124 }
e9be9d5e
MS
125 error = vfs_setxattr(new, name, value, size, 0);
126 if (error)
e4ad29fa 127 break;
e9be9d5e 128 }
e9be9d5e
MS
129 kfree(value);
130out:
131 kfree(buf);
132 return error;
133}
134
135static int ovl_copy_up_data(struct path *old, struct path *new, loff_t len)
136{
137 struct file *old_file;
138 struct file *new_file;
139 loff_t old_pos = 0;
140 loff_t new_pos = 0;
141 int error = 0;
142
143 if (len == 0)
144 return 0;
145
0480334f 146 old_file = ovl_path_open(old, O_LARGEFILE | O_RDONLY);
e9be9d5e
MS
147 if (IS_ERR(old_file))
148 return PTR_ERR(old_file);
149
0480334f 150 new_file = ovl_path_open(new, O_LARGEFILE | O_WRONLY);
e9be9d5e
MS
151 if (IS_ERR(new_file)) {
152 error = PTR_ERR(new_file);
153 goto out_fput;
154 }
155
2ea98466
AG
156 /* Try to use clone_file_range to clone up within the same fs */
157 error = vfs_clone_file_range(old_file, 0, new_file, 0, len);
158 if (!error)
159 goto out;
160 /* Couldn't clone, so now we try to copy the data */
161 error = 0;
162
e9be9d5e
MS
163 /* FIXME: copy up sparse files efficiently */
164 while (len) {
165 size_t this_len = OVL_COPY_UP_CHUNK_SIZE;
166 long bytes;
167
168 if (len < this_len)
169 this_len = len;
170
171 if (signal_pending_state(TASK_KILLABLE, current)) {
172 error = -EINTR;
173 break;
174 }
175
176 bytes = do_splice_direct(old_file, &old_pos,
177 new_file, &new_pos,
178 this_len, SPLICE_F_MOVE);
179 if (bytes <= 0) {
180 error = bytes;
181 break;
182 }
183 WARN_ON(old_pos != new_pos);
184
185 len -= bytes;
186 }
2ea98466 187out:
641089c1
MS
188 if (!error)
189 error = vfs_fsync(new_file, 0);
e9be9d5e
MS
190 fput(new_file);
191out_fput:
192 fput(old_file);
193 return error;
194}
195
e9be9d5e
MS
196static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat)
197{
198 struct iattr attr = {
199 .ia_valid =
200 ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET,
201 .ia_atime = stat->atime,
202 .ia_mtime = stat->mtime,
203 };
204
205 return notify_change(upperdentry, &attr, NULL);
206}
207
208int ovl_set_attr(struct dentry *upperdentry, struct kstat *stat)
209{
210 int err = 0;
211
212 if (!S_ISLNK(stat->mode)) {
213 struct iattr attr = {
214 .ia_valid = ATTR_MODE,
215 .ia_mode = stat->mode,
216 };
217 err = notify_change(upperdentry, &attr, NULL);
218 }
219 if (!err) {
220 struct iattr attr = {
221 .ia_valid = ATTR_UID | ATTR_GID,
222 .ia_uid = stat->uid,
223 .ia_gid = stat->gid,
224 };
225 err = notify_change(upperdentry, &attr, NULL);
226 }
227 if (!err)
228 ovl_set_timestamps(upperdentry, stat);
229
230 return err;
e9be9d5e
MS
231}
232
233static int ovl_copy_up_locked(struct dentry *workdir, struct dentry *upperdir,
234 struct dentry *dentry, struct path *lowerpath,
0f7ff2da 235 struct kstat *stat, const char *link)
e9be9d5e
MS
236{
237 struct inode *wdir = workdir->d_inode;
238 struct inode *udir = upperdir->d_inode;
239 struct dentry *newdentry = NULL;
240 struct dentry *upper = NULL;
e9be9d5e 241 int err;
d8ad8b49
VG
242 const struct cred *old_creds = NULL;
243 struct cred *new_creds = NULL;
32a3d848
AV
244 struct cattr cattr = {
245 /* Can't properly set mode on creation because of the umask */
246 .mode = stat->mode & S_IFMT,
247 .rdev = stat->rdev,
248 .link = link
249 };
e9be9d5e
MS
250
251 newdentry = ovl_lookup_temp(workdir, dentry);
252 err = PTR_ERR(newdentry);
253 if (IS_ERR(newdentry))
254 goto out;
255
256 upper = lookup_one_len(dentry->d_name.name, upperdir,
257 dentry->d_name.len);
258 err = PTR_ERR(upper);
259 if (IS_ERR(upper))
260 goto out1;
261
d8ad8b49
VG
262 err = security_inode_copy_up(dentry, &new_creds);
263 if (err < 0)
264 goto out2;
265
266 if (new_creds)
267 old_creds = override_creds(new_creds);
268
32a3d848 269 err = ovl_create_real(wdir, newdentry, &cattr, NULL, true);
d8ad8b49
VG
270
271 if (new_creds) {
272 revert_creds(old_creds);
273 put_cred(new_creds);
274 }
275
e9be9d5e
MS
276 if (err)
277 goto out2;
278
279 if (S_ISREG(stat->mode)) {
280 struct path upperpath;
f134f244 281
e9be9d5e
MS
282 ovl_path_upper(dentry, &upperpath);
283 BUG_ON(upperpath.dentry != NULL);
284 upperpath.dentry = newdentry;
285
286 err = ovl_copy_up_data(lowerpath, &upperpath, stat->size);
287 if (err)
288 goto out_cleanup;
289 }
290
291 err = ovl_copy_xattr(lowerpath->dentry, newdentry);
292 if (err)
293 goto out_cleanup;
294
5955102c 295 inode_lock(newdentry->d_inode);
e9be9d5e 296 err = ovl_set_attr(newdentry, stat);
5955102c 297 inode_unlock(newdentry->d_inode);
e9be9d5e
MS
298 if (err)
299 goto out_cleanup;
300
301 err = ovl_do_rename(wdir, newdentry, udir, upper, 0);
302 if (err)
303 goto out_cleanup;
304
305 ovl_dentry_update(dentry, newdentry);
39b681f8 306 ovl_inode_update(d_inode(dentry), d_inode(newdentry));
e9be9d5e 307 newdentry = NULL;
e9be9d5e
MS
308out2:
309 dput(upper);
310out1:
311 dput(newdentry);
312out:
313 return err;
314
315out_cleanup:
316 ovl_cleanup(wdir, newdentry);
ab79efab 317 goto out2;
e9be9d5e
MS
318}
319
320/*
321 * Copy up a single dentry
322 *
a6c60655
MS
323 * All renames start with copy up of source if necessary. The actual
324 * rename will only proceed once the copy up was successful. Copy up uses
325 * upper parent i_mutex for exclusion. Since rename can change d_parent it
326 * is possible that the copy up will lock the old parent. At that point
327 * the file will have already been copied up anyway.
e9be9d5e 328 */
9aba6521
AG
329static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
330 struct path *lowerpath, struct kstat *stat)
e9be9d5e 331{
7764235b 332 DEFINE_DELAYED_CALL(done);
e9be9d5e
MS
333 struct dentry *workdir = ovl_workdir(dentry);
334 int err;
335 struct kstat pstat;
336 struct path parentpath;
7764235b 337 struct dentry *lowerdentry = lowerpath->dentry;
e9be9d5e 338 struct dentry *upperdir;
7764235b 339 const char *link = NULL;
e9be9d5e 340
cc6f67bc
MS
341 if (WARN_ON(!workdir))
342 return -EROFS;
343
7764235b 344 ovl_do_check_copy_up(lowerdentry);
fb5bb2c3 345
e9be9d5e
MS
346 ovl_path_upper(parent, &parentpath);
347 upperdir = parentpath.dentry;
348
349 err = vfs_getattr(&parentpath, &pstat);
350 if (err)
351 return err;
352
353 if (S_ISLNK(stat->mode)) {
7764235b 354 link = vfs_get_link(lowerdentry, &done);
e9be9d5e
MS
355 if (IS_ERR(link))
356 return PTR_ERR(link);
357 }
358
e9be9d5e
MS
359 err = -EIO;
360 if (lock_rename(workdir, upperdir) != NULL) {
361 pr_err("overlayfs: failed to lock workdir+upperdir\n");
362 goto out_unlock;
363 }
a6c60655 364 if (ovl_dentry_upper(dentry)) {
0f7ff2da 365 /* Raced with another copy-up? Nothing to do, then... */
e9be9d5e 366 err = 0;
0f7ff2da 367 goto out_unlock;
e9be9d5e
MS
368 }
369
370 err = ovl_copy_up_locked(workdir, upperdir, dentry, lowerpath,
0f7ff2da 371 stat, link);
e9be9d5e
MS
372 if (!err) {
373 /* Restore timestamps on parent (best effort) */
374 ovl_set_timestamps(upperdir, &pstat);
375 }
376out_unlock:
377 unlock_rename(workdir, upperdir);
7764235b 378 do_delayed_call(&done);
e9be9d5e
MS
379
380 return err;
381}
382
9aba6521 383int ovl_copy_up_flags(struct dentry *dentry, int flags)
e9be9d5e 384{
8eac98b8
VG
385 int err = 0;
386 const struct cred *old_cred = ovl_override_creds(dentry->d_sb);
e9be9d5e 387
e9be9d5e
MS
388 while (!err) {
389 struct dentry *next;
390 struct dentry *parent;
391 struct path lowerpath;
392 struct kstat stat;
393 enum ovl_path_type type = ovl_path_type(dentry);
394
1afaba1e 395 if (OVL_TYPE_UPPER(type))
e9be9d5e
MS
396 break;
397
398 next = dget(dentry);
399 /* find the topmost dentry not yet copied up */
400 for (;;) {
401 parent = dget_parent(next);
402
403 type = ovl_path_type(parent);
1afaba1e 404 if (OVL_TYPE_UPPER(type))
e9be9d5e
MS
405 break;
406
407 dput(next);
408 next = parent;
409 }
410
411 ovl_path_lower(next, &lowerpath);
412 err = vfs_getattr(&lowerpath, &stat);
9aba6521
AG
413 /* maybe truncate regular file. this has no effect on dirs */
414 if (flags & O_TRUNC)
415 stat.size = 0;
e9be9d5e 416 if (!err)
0f7ff2da 417 err = ovl_copy_up_one(parent, next, &lowerpath, &stat);
e9be9d5e
MS
418
419 dput(parent);
420 dput(next);
421 }
8eac98b8 422 revert_creds(old_cred);
e9be9d5e
MS
423
424 return err;
425}
9aba6521
AG
426
427int ovl_copy_up(struct dentry *dentry)
428{
429 return ovl_copy_up_flags(dentry, 0);
430}