ovl: move copy up lock out
[linux-block.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>
174cd4b1 18#include <linux/sched/signal.h>
5b825c3a 19#include <linux/cred.h>
e9be9d5e 20#include <linux/namei.h>
fb5bb2c3
DH
21#include <linux/fdtable.h>
22#include <linux/ratelimit.h>
3a1e819b 23#include <linux/exportfs.h>
e9be9d5e 24#include "overlayfs.h"
d8514d8e 25#include "ovl_entry.h"
e9be9d5e
MS
26
27#define OVL_COPY_UP_CHUNK_SIZE (1 << 20)
28
fb5bb2c3
DH
29static bool __read_mostly ovl_check_copy_up;
30module_param_named(check_copy_up, ovl_check_copy_up, bool,
31 S_IWUSR | S_IRUGO);
32MODULE_PARM_DESC(ovl_check_copy_up,
33 "Warn on copy-up when causing process also has a R/O fd open");
34
35static int ovl_check_fd(const void *data, struct file *f, unsigned int fd)
36{
37 const struct dentry *dentry = data;
38
45063097 39 if (file_inode(f) == d_inode(dentry))
fb5bb2c3
DH
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);
42 return 0;
43}
44
45/*
46 * Check the fds open by this process and warn if something like the following
47 * scenario is about to occur:
48 *
49 * fd1 = open("foo", O_RDONLY);
50 * fd2 = open("foo", O_RDWR);
51 */
52static void ovl_do_check_copy_up(struct dentry *dentry)
53{
54 if (ovl_check_copy_up)
55 iterate_fd(current->files, 0, ovl_check_fd, dentry);
56}
57
e9be9d5e
MS
58int ovl_copy_xattr(struct dentry *old, struct dentry *new)
59{
e4ad29fa
VC
60 ssize_t list_size, size, value_size = 0;
61 char *buf, *name, *value = NULL;
62 int uninitialized_var(error);
8b326c61 63 size_t slen;
e9be9d5e 64
5d6c3191
AG
65 if (!(old->d_inode->i_opflags & IOP_XATTR) ||
66 !(new->d_inode->i_opflags & IOP_XATTR))
e9be9d5e
MS
67 return 0;
68
69 list_size = vfs_listxattr(old, NULL, 0);
70 if (list_size <= 0) {
71 if (list_size == -EOPNOTSUPP)
72 return 0;
73 return list_size;
74 }
75
76 buf = kzalloc(list_size, GFP_KERNEL);
77 if (!buf)
78 return -ENOMEM;
79
e9be9d5e
MS
80 list_size = vfs_listxattr(old, buf, list_size);
81 if (list_size <= 0) {
82 error = list_size;
e4ad29fa 83 goto out;
e9be9d5e
MS
84 }
85
8b326c61
MS
86 for (name = buf; list_size; name += slen) {
87 slen = strnlen(name, list_size) + 1;
88
89 /* underlying fs providing us with an broken xattr list? */
90 if (WARN_ON(slen > list_size)) {
91 error = -EIO;
92 break;
93 }
94 list_size -= slen;
95
0956254a
MS
96 if (ovl_is_private_xattr(name))
97 continue;
e4ad29fa
VC
98retry:
99 size = vfs_getxattr(old, name, value, value_size);
100 if (size == -ERANGE)
101 size = vfs_getxattr(old, name, NULL, 0);
102
97daf8b9 103 if (size < 0) {
e9be9d5e 104 error = size;
e4ad29fa 105 break;
e9be9d5e 106 }
e4ad29fa
VC
107
108 if (size > value_size) {
109 void *new;
110
111 new = krealloc(value, size, GFP_KERNEL);
112 if (!new) {
113 error = -ENOMEM;
114 break;
115 }
116 value = new;
117 value_size = size;
118 goto retry;
119 }
120
121ab822
VG
121 error = security_inode_copy_up_xattr(name);
122 if (error < 0 && error != -EOPNOTSUPP)
123 break;
124 if (error == 1) {
125 error = 0;
126 continue; /* Discard */
127 }
e9be9d5e
MS
128 error = vfs_setxattr(new, name, value, size, 0);
129 if (error)
e4ad29fa 130 break;
e9be9d5e 131 }
e9be9d5e
MS
132 kfree(value);
133out:
134 kfree(buf);
135 return error;
136}
137
138static int ovl_copy_up_data(struct path *old, struct path *new, loff_t len)
139{
140 struct file *old_file;
141 struct file *new_file;
142 loff_t old_pos = 0;
143 loff_t new_pos = 0;
144 int error = 0;
145
146 if (len == 0)
147 return 0;
148
0480334f 149 old_file = ovl_path_open(old, O_LARGEFILE | O_RDONLY);
e9be9d5e
MS
150 if (IS_ERR(old_file))
151 return PTR_ERR(old_file);
152
0480334f 153 new_file = ovl_path_open(new, O_LARGEFILE | O_WRONLY);
e9be9d5e
MS
154 if (IS_ERR(new_file)) {
155 error = PTR_ERR(new_file);
156 goto out_fput;
157 }
158
2ea98466
AG
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);
161 if (!error)
162 goto out;
163 /* Couldn't clone, so now we try to copy the data */
164 error = 0;
165
e9be9d5e
MS
166 /* FIXME: copy up sparse files efficiently */
167 while (len) {
168 size_t this_len = OVL_COPY_UP_CHUNK_SIZE;
169 long bytes;
170
171 if (len < this_len)
172 this_len = len;
173
174 if (signal_pending_state(TASK_KILLABLE, current)) {
175 error = -EINTR;
176 break;
177 }
178
179 bytes = do_splice_direct(old_file, &old_pos,
180 new_file, &new_pos,
181 this_len, SPLICE_F_MOVE);
182 if (bytes <= 0) {
183 error = bytes;
184 break;
185 }
186 WARN_ON(old_pos != new_pos);
187
188 len -= bytes;
189 }
2ea98466 190out:
641089c1
MS
191 if (!error)
192 error = vfs_fsync(new_file, 0);
e9be9d5e
MS
193 fput(new_file);
194out_fput:
195 fput(old_file);
196 return error;
197}
198
e9be9d5e
MS
199static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat)
200{
201 struct iattr attr = {
202 .ia_valid =
203 ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET,
204 .ia_atime = stat->atime,
205 .ia_mtime = stat->mtime,
206 };
207
208 return notify_change(upperdentry, &attr, NULL);
209}
210
211int ovl_set_attr(struct dentry *upperdentry, struct kstat *stat)
212{
213 int err = 0;
214
215 if (!S_ISLNK(stat->mode)) {
216 struct iattr attr = {
217 .ia_valid = ATTR_MODE,
218 .ia_mode = stat->mode,
219 };
220 err = notify_change(upperdentry, &attr, NULL);
221 }
222 if (!err) {
223 struct iattr attr = {
224 .ia_valid = ATTR_UID | ATTR_GID,
225 .ia_uid = stat->uid,
226 .ia_gid = stat->gid,
227 };
228 err = notify_change(upperdentry, &attr, NULL);
229 }
230 if (!err)
231 ovl_set_timestamps(upperdentry, stat);
232
233 return err;
e9be9d5e
MS
234}
235
54fb347e 236struct ovl_fh *ovl_encode_fh(struct dentry *lower, bool is_upper)
3a1e819b
AG
237{
238 struct ovl_fh *fh;
239 int fh_type, fh_len, dwords;
240 void *buf;
241 int buflen = MAX_HANDLE_SZ;
02bcd157 242 uuid_t *uuid = &lower->d_sb->s_uuid;
3a1e819b
AG
243
244 buf = kmalloc(buflen, GFP_TEMPORARY);
245 if (!buf)
246 return ERR_PTR(-ENOMEM);
247
248 /*
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.
252 */
253 dwords = buflen >> 2;
254 fh_type = exportfs_encode_fh(lower, buf, &dwords, 0);
255 buflen = (dwords << 2);
256
257 fh = ERR_PTR(-EIO);
258 if (WARN_ON(fh_type < 0) ||
259 WARN_ON(buflen > MAX_HANDLE_SZ) ||
260 WARN_ON(fh_type == FILEID_INVALID))
261 goto out;
262
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);
266 if (!fh) {
267 fh = ERR_PTR(-ENOMEM);
268 goto out;
269 }
270
271 fh->version = OVL_FH_VERSION;
272 fh->magic = OVL_FH_MAGIC;
273 fh->type = fh_type;
274 fh->flags = OVL_FH_FLAG_CPU_ENDIAN;
54fb347e
AG
275 /*
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.
280 */
281 if (is_upper)
282 fh->flags |= OVL_FH_FLAG_PATH_UPPER;
3a1e819b
AG
283 fh->len = fh_len;
284 fh->uuid = *uuid;
285 memcpy(fh->fid, buf, buflen);
286
287out:
288 kfree(buf);
289 return fh;
290}
291
292static int ovl_set_origin(struct dentry *dentry, struct dentry *lower,
293 struct dentry *upper)
294{
3a1e819b
AG
295 const struct ovl_fh *fh = NULL;
296 int err;
297
298 /*
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.
302 */
02bcd157 303 if (ovl_can_decode_fh(lower->d_sb)) {
54fb347e 304 fh = ovl_encode_fh(lower, false);
3a1e819b
AG
305 if (IS_ERR(fh))
306 return PTR_ERR(fh);
307 }
308
6266d465
MS
309 /*
310 * Do not fail when upper doesn't support xattrs.
311 */
312 err = ovl_check_setxattr(dentry, upper, OVL_XATTR_ORIGIN, fh,
313 fh ? fh->len : 0, 0);
3a1e819b
AG
314 kfree(fh);
315
316 return err;
317}
318
23f0ab13 319struct ovl_copy_up_ctx {
a6fb235a 320 struct dentry *parent;
23f0ab13
MS
321 struct dentry *dentry;
322 struct path lowerpath;
323 struct kstat stat;
324 struct kstat pstat;
325 const char *link;
326 struct dentry *upperdir;
327 struct dentry *workdir;
328 bool tmpfile;
329};
330
331static int ovl_install_temp(struct ovl_copy_up_ctx *c, struct dentry *temp,
332 struct dentry **newdentry)
15932c41
AG
333{
334 int err;
335 struct dentry *upper;
23f0ab13 336 struct inode *udir = d_inode(c->upperdir);
15932c41 337
23f0ab13
MS
338 upper = lookup_one_len(c->dentry->d_name.name, c->upperdir,
339 c->dentry->d_name.len);
15932c41
AG
340 if (IS_ERR(upper))
341 return PTR_ERR(upper);
342
23f0ab13 343 if (c->tmpfile)
15932c41
AG
344 err = ovl_do_link(temp, udir, upper, true);
345 else
23f0ab13 346 err = ovl_do_rename(d_inode(c->workdir), temp, udir, upper, 0);
15932c41
AG
347
348 /* Restore timestamps on parent (best effort) */
349 if (!err) {
23f0ab13
MS
350 ovl_set_timestamps(c->upperdir, &c->pstat);
351 *newdentry = dget(c->tmpfile ? upper : temp);
15932c41
AG
352 }
353 dput(upper);
354
355 return err;
356}
357
23f0ab13 358static int ovl_get_tmpfile(struct ovl_copy_up_ctx *c, struct dentry **tempp)
e9be9d5e 359{
e9be9d5e 360 int err;
7d90b853 361 struct dentry *temp;
d8ad8b49
VG
362 const struct cred *old_creds = NULL;
363 struct cred *new_creds = NULL;
32a3d848
AV
364 struct cattr cattr = {
365 /* Can't properly set mode on creation because of the umask */
23f0ab13
MS
366 .mode = c->stat.mode & S_IFMT,
367 .rdev = c->stat.rdev,
368 .link = c->link
32a3d848 369 };
e9be9d5e 370
23f0ab13 371 err = security_inode_copy_up(c->dentry, &new_creds);
d8ad8b49 372 if (err < 0)
e85f82ff 373 goto out;
d8ad8b49
VG
374
375 if (new_creds)
376 old_creds = override_creds(new_creds);
377
23f0ab13
MS
378 if (c->tmpfile) {
379 temp = ovl_do_tmpfile(c->workdir, c->stat.mode);
7d90b853
MS
380 if (IS_ERR(temp))
381 goto temp_err;
382 } else {
23f0ab13 383 temp = ovl_lookup_temp(c->workdir);
7d90b853
MS
384 if (IS_ERR(temp))
385 goto temp_err;
386
23f0ab13 387 err = ovl_create_real(d_inode(c->workdir), temp, &cattr,
7d90b853
MS
388 NULL, true);
389 if (err) {
390 dput(temp);
391 goto out;
392 }
8137ae26 393 }
7d90b853
MS
394 err = 0;
395 *tempp = temp;
396out:
d8ad8b49
VG
397 if (new_creds) {
398 revert_creds(old_creds);
399 put_cred(new_creds);
400 }
401
7d90b853
MS
402 return err;
403
404temp_err:
405 err = PTR_ERR(temp);
406 goto out;
407}
408
23f0ab13 409static int ovl_copy_up_inode(struct ovl_copy_up_ctx *c, struct dentry *temp)
7d90b853 410{
7d90b853
MS
411 int err;
412
23f0ab13 413 if (S_ISREG(c->stat.mode)) {
e9be9d5e 414 struct path upperpath;
f134f244 415
23f0ab13 416 ovl_path_upper(c->dentry, &upperpath);
e9be9d5e 417 BUG_ON(upperpath.dentry != NULL);
42f269b9 418 upperpath.dentry = temp;
e9be9d5e 419
23f0ab13 420 err = ovl_copy_up_data(&c->lowerpath, &upperpath, c->stat.size);
e9be9d5e 421 if (err)
02209d10 422 return err;
e9be9d5e
MS
423 }
424
23f0ab13 425 err = ovl_copy_xattr(c->lowerpath.dentry, temp);
e9be9d5e 426 if (err)
02209d10 427 return err;
e9be9d5e 428
42f269b9 429 inode_lock(temp->d_inode);
23f0ab13 430 err = ovl_set_attr(temp, &c->stat);
42f269b9 431 inode_unlock(temp->d_inode);
e9be9d5e 432 if (err)
02209d10 433 return err;
e9be9d5e 434
3a1e819b
AG
435 /*
436 * Store identifier of lower inode in upper inode xattr to
437 * allow lookup of the copy up origin inode.
fbaf94ee
MS
438 *
439 * Don't set origin when we are breaking the association with a lower
440 * hard link.
3a1e819b 441 */
23f0ab13
MS
442 if (S_ISDIR(c->stat.mode) || c->stat.nlink == 1) {
443 err = ovl_set_origin(c->dentry, c->lowerpath.dentry, temp);
fbaf94ee 444 if (err)
02209d10 445 return err;
fbaf94ee 446 }
3a1e819b 447
02209d10
AG
448 return 0;
449}
450
23f0ab13 451static int ovl_copy_up_locked(struct ovl_copy_up_ctx *c)
02209d10 452{
23f0ab13 453 struct inode *udir = c->upperdir->d_inode;
02209d10
AG
454 struct dentry *newdentry = NULL;
455 struct dentry *temp = NULL;
456 int err;
457
23f0ab13 458 err = ovl_get_tmpfile(c, &temp);
02209d10
AG
459 if (err)
460 goto out;
461
23f0ab13 462 err = ovl_copy_up_inode(c, temp);
02209d10
AG
463 if (err)
464 goto out_cleanup;
465
23f0ab13 466 if (c->tmpfile) {
15932c41 467 inode_lock_nested(udir, I_MUTEX_PARENT);
23f0ab13 468 err = ovl_install_temp(c, temp, &newdentry);
15932c41
AG
469 inode_unlock(udir);
470 } else {
23f0ab13 471 err = ovl_install_temp(c, temp, &newdentry);
e85f82ff 472 }
e9be9d5e
MS
473 if (err)
474 goto out_cleanup;
475
55acc661 476 ovl_dentry_set_upper_alias(c->dentry);
23f0ab13 477 ovl_inode_update(d_inode(c->dentry), newdentry);
e85f82ff 478out:
42f269b9 479 dput(temp);
e9be9d5e
MS
480 return err;
481
482out_cleanup:
23f0ab13
MS
483 if (!c->tmpfile)
484 ovl_cleanup(d_inode(c->workdir), temp);
e85f82ff 485 goto out;
e9be9d5e
MS
486}
487
488/*
489 * Copy up a single dentry
490 *
a6c60655
MS
491 * All renames start with copy up of source if necessary. The actual
492 * rename will only proceed once the copy up was successful. Copy up uses
493 * upper parent i_mutex for exclusion. Since rename can change d_parent it
494 * is possible that the copy up will lock the old parent. At that point
495 * the file will have already been copied up anyway.
e9be9d5e 496 */
a6fb235a 497static int ovl_do_copy_up(struct ovl_copy_up_ctx *c)
e9be9d5e 498{
e9be9d5e 499 int err;
23f0ab13 500 struct ovl_fs *ofs = c->dentry->d_sb->s_fs_info;
e9be9d5e 501
f3a15685 502 /* Mark parent "impure" because it may now contain non-pure upper */
a6fb235a 503 err = ovl_set_impure(c->parent, c->upperdir);
e9be9d5e
MS
504 if (err)
505 return err;
506
01ad3eb8 507 /* Should we copyup with O_TMPFILE or with workdir? */
23f0ab13 508 if (S_ISREG(c->stat.mode) && ofs->tmpfile) {
23f0ab13 509 c->tmpfile = true;
fd210b7d 510 return ovl_copy_up_locked(c);
01ad3eb8
AG
511 }
512
e9be9d5e 513 err = -EIO;
23f0ab13 514 if (lock_rename(c->workdir, c->upperdir) != NULL) {
e9be9d5e 515 pr_err("overlayfs: failed to lock workdir+upperdir\n");
fd210b7d
MS
516 } else {
517 err = ovl_copy_up_locked(c);
518 unlock_rename(c->workdir, c->upperdir);
e9be9d5e
MS
519 }
520
a6fb235a
MS
521 return err;
522}
523
524static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
525 int flags)
526{
527 int err;
528 DEFINE_DELAYED_CALL(done);
529 struct path parentpath;
530 struct ovl_copy_up_ctx ctx = {
531 .parent = parent,
532 .dentry = dentry,
533 .workdir = ovl_workdir(dentry),
534 };
535
536 if (WARN_ON(!ctx.workdir))
537 return -EROFS;
538
539 ovl_path_lower(dentry, &ctx.lowerpath);
540 err = vfs_getattr(&ctx.lowerpath, &ctx.stat,
541 STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
542 if (err)
543 return err;
544
545 ovl_path_upper(parent, &parentpath);
546 ctx.upperdir = parentpath.dentry;
547
548 err = vfs_getattr(&parentpath, &ctx.pstat,
549 STATX_ATIME | STATX_MTIME, AT_STATX_SYNC_AS_STAT);
550 if (err)
551 return err;
552
553 /* maybe truncate regular file. this has no effect on dirs */
554 if (flags & O_TRUNC)
555 ctx.stat.size = 0;
556
557 if (S_ISLNK(ctx.stat.mode)) {
558 ctx.link = vfs_get_link(ctx.lowerpath.dentry, &done);
559 if (IS_ERR(ctx.link))
560 return PTR_ERR(ctx.link);
561 }
562 ovl_do_check_copy_up(ctx.lowerpath.dentry);
563
fd210b7d
MS
564 err = ovl_copy_up_start(dentry);
565 /* err < 0: interrupted, err > 0: raced with another copy-up */
566 if (unlikely(err)) {
567 if (err > 0)
568 err = 0;
569 } else {
570 err = ovl_do_copy_up(&ctx);
571 ovl_copy_up_end(dentry);
572 }
7764235b 573 do_delayed_call(&done);
e9be9d5e
MS
574
575 return err;
576}
577
9aba6521 578int ovl_copy_up_flags(struct dentry *dentry, int flags)
e9be9d5e 579{
8eac98b8
VG
580 int err = 0;
581 const struct cred *old_cred = ovl_override_creds(dentry->d_sb);
e9be9d5e 582
e9be9d5e
MS
583 while (!err) {
584 struct dentry *next;
585 struct dentry *parent;
e9be9d5e
MS
586 enum ovl_path_type type = ovl_path_type(dentry);
587
1afaba1e 588 if (OVL_TYPE_UPPER(type))
e9be9d5e
MS
589 break;
590
591 next = dget(dentry);
592 /* find the topmost dentry not yet copied up */
593 for (;;) {
594 parent = dget_parent(next);
595
596 type = ovl_path_type(parent);
1afaba1e 597 if (OVL_TYPE_UPPER(type))
e9be9d5e
MS
598 break;
599
600 dput(next);
601 next = parent;
602 }
603
a6fb235a 604 err = ovl_copy_up_one(parent, next, flags);
e9be9d5e
MS
605
606 dput(parent);
607 dput(next);
608 }
8eac98b8 609 revert_creds(old_cred);
e9be9d5e
MS
610
611 return err;
612}
9aba6521
AG
613
614int ovl_copy_up(struct dentry *dentry)
615{
616 return ovl_copy_up_flags(dentry, 0);
617}