f2fs: Provide a splice-read wrapper
[linux-block.git] / fs / overlayfs / copy_up.c
CommitLineData
d2912cb1 1// SPDX-License-Identifier: GPL-2.0-only
e9be9d5e
MS
2/*
3 *
4 * Copyright (C) 2011 Novell Inc.
e9be9d5e
MS
5 */
6
fb5bb2c3 7#include <linux/module.h>
e9be9d5e
MS
8#include <linux/fs.h>
9#include <linux/slab.h>
10#include <linux/file.h>
72db8211 11#include <linux/fileattr.h>
e9be9d5e
MS
12#include <linux/splice.h>
13#include <linux/xattr.h>
14#include <linux/security.h>
15#include <linux/uaccess.h>
174cd4b1 16#include <linux/sched/signal.h>
5b825c3a 17#include <linux/cred.h>
e9be9d5e 18#include <linux/namei.h>
fb5bb2c3
DH
19#include <linux/fdtable.h>
20#include <linux/ratelimit.h>
3a1e819b 21#include <linux/exportfs.h>
e9be9d5e
MS
22#include "overlayfs.h"
23
24#define OVL_COPY_UP_CHUNK_SIZE (1 << 20)
25
670c2324 26static int ovl_ccup_set(const char *buf, const struct kernel_param *param)
fb5bb2c3 27{
1bd0a3ae 28 pr_warn("\"check_copy_up\" module option is obsolete\n");
fb5bb2c3
DH
29 return 0;
30}
31
670c2324 32static int ovl_ccup_get(char *buf, const struct kernel_param *param)
fb5bb2c3 33{
670c2324 34 return sprintf(buf, "N\n");
fb5bb2c3
DH
35}
36
670c2324 37module_param_call(check_copy_up, ovl_ccup_set, ovl_ccup_get, NULL, 0644);
253e7483 38MODULE_PARM_DESC(check_copy_up, "Obsolete; does nothing");
670c2324 39
c61ca557
MS
40static bool ovl_must_copy_xattr(const char *name)
41{
42 return !strcmp(name, XATTR_POSIX_ACL_ACCESS) ||
43 !strcmp(name, XATTR_POSIX_ACL_DEFAULT) ||
44 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN);
45}
46
31acceb9
CB
47static int ovl_copy_acl(struct ovl_fs *ofs, const struct path *path,
48 struct dentry *dentry, const char *acl_name)
49{
50 int err;
51 struct posix_acl *clone, *real_acl = NULL;
52
53 real_acl = ovl_get_acl_path(path, acl_name, false);
54 if (!real_acl)
55 return 0;
56
57 if (IS_ERR(real_acl)) {
58 err = PTR_ERR(real_acl);
59 if (err == -ENODATA || err == -EOPNOTSUPP)
60 return 0;
61 return err;
62 }
63
64 clone = posix_acl_clone(real_acl, GFP_KERNEL);
65 posix_acl_release(real_acl); /* release original acl */
66 if (!clone)
67 return -ENOMEM;
68
69 err = ovl_do_set_acl(ofs, dentry, acl_name, clone);
70
71 /* release cloned acl */
72 posix_acl_release(clone);
73 return err;
74}
75
2d343087 76int ovl_copy_xattr(struct super_block *sb, const struct path *oldpath, struct dentry *new)
e9be9d5e 77{
dad7017a 78 struct dentry *old = oldpath->dentry;
e4ad29fa
VC
79 ssize_t list_size, size, value_size = 0;
80 char *buf, *name, *value = NULL;
520da69d 81 int error = 0;
8b326c61 82 size_t slen;
e9be9d5e 83
a1fbb607 84 if (!old->d_inode->i_op->listxattr || !new->d_inode->i_op->listxattr)
e9be9d5e
MS
85 return 0;
86
87 list_size = vfs_listxattr(old, NULL, 0);
88 if (list_size <= 0) {
89 if (list_size == -EOPNOTSUPP)
90 return 0;
91 return list_size;
92 }
93
f945ca19 94 buf = kvzalloc(list_size, GFP_KERNEL);
e9be9d5e
MS
95 if (!buf)
96 return -ENOMEM;
97
e9be9d5e
MS
98 list_size = vfs_listxattr(old, buf, list_size);
99 if (list_size <= 0) {
100 error = list_size;
e4ad29fa 101 goto out;
e9be9d5e
MS
102 }
103
8b326c61
MS
104 for (name = buf; list_size; name += slen) {
105 slen = strnlen(name, list_size) + 1;
106
107 /* underlying fs providing us with an broken xattr list? */
108 if (WARN_ON(slen > list_size)) {
109 error = -EIO;
110 break;
111 }
112 list_size -= slen;
113
610afc0b 114 if (ovl_is_private_xattr(sb, name))
0956254a 115 continue;
03fedf93
AG
116
117 error = security_inode_copy_up_xattr(name);
118 if (error < 0 && error != -EOPNOTSUPP)
119 break;
120 if (error == 1) {
121 error = 0;
122 continue; /* Discard */
123 }
31acceb9
CB
124
125 if (is_posix_acl_xattr(name)) {
126 error = ovl_copy_acl(OVL_FS(sb), oldpath, new, name);
127 if (!error)
128 continue;
129 /* POSIX ACLs must be copied. */
130 break;
131 }
132
e4ad29fa 133retry:
dad7017a 134 size = ovl_do_getxattr(oldpath, name, value, value_size);
e4ad29fa 135 if (size == -ERANGE)
dad7017a 136 size = ovl_do_getxattr(oldpath, name, NULL, 0);
e4ad29fa 137
97daf8b9 138 if (size < 0) {
e9be9d5e 139 error = size;
e4ad29fa 140 break;
e9be9d5e 141 }
e4ad29fa
VC
142
143 if (size > value_size) {
144 void *new;
145
f945ca19 146 new = kvmalloc(size, GFP_KERNEL);
e4ad29fa
VC
147 if (!new) {
148 error = -ENOMEM;
149 break;
150 }
f945ca19 151 kvfree(value);
e4ad29fa
VC
152 value = new;
153 value_size = size;
154 goto retry;
155 }
156
c914c0e2 157 error = ovl_do_setxattr(OVL_FS(sb), new, name, value, size, 0);
c61ca557
MS
158 if (error) {
159 if (error != -EOPNOTSUPP || ovl_must_copy_xattr(name))
160 break;
161
162 /* Ignore failure to copy unknown xattrs */
163 error = 0;
164 }
e9be9d5e 165 }
f945ca19 166 kvfree(value);
e9be9d5e 167out:
f945ca19 168 kvfree(buf);
e9be9d5e
MS
169 return error;
170}
171
2d343087
AV
172static int ovl_copy_fileattr(struct inode *inode, const struct path *old,
173 const struct path *new)
72db8211
AG
174{
175 struct fileattr oldfa = { .flags_valid = true };
176 struct fileattr newfa = { .flags_valid = true };
177 int err;
178
179 err = ovl_real_fileattr_get(old, &oldfa);
5b0a414d
MS
180 if (err) {
181 /* Ntfs-3g returns -EINVAL for "no fileattr support" */
182 if (err == -ENOTTY || err == -EINVAL)
183 return 0;
184 pr_warn("failed to retrieve lower fileattr (%pd2, err=%i)\n",
4ee7e4a6 185 old->dentry, err);
72db8211 186 return err;
5b0a414d 187 }
72db8211 188
096a218a
AG
189 /*
190 * We cannot set immutable and append-only flags on upper inode,
191 * because we would not be able to link upper inode to upper dir
192 * not set overlay private xattr on upper inode.
193 * Store these flags in overlay.protattr xattr instead.
194 */
195 if (oldfa.flags & OVL_PROT_FS_FLAGS_MASK) {
196 err = ovl_set_protattr(inode, new->dentry, &oldfa);
94fd1975
MS
197 if (err == -EPERM)
198 pr_warn_once("copying fileattr: no xattr on upper\n");
199 else if (err)
096a218a
AG
200 return err;
201 }
202
5b0a414d
MS
203 /* Don't bother copying flags if none are set */
204 if (!(oldfa.flags & OVL_COPY_FS_FLAGS_MASK))
205 return 0;
206
207 err = ovl_real_fileattr_get(new, &newfa);
208 if (err) {
94fd1975
MS
209 /*
210 * Returning an error if upper doesn't support fileattr will
211 * result in a regression, so revert to the old behavior.
212 */
213 if (err == -ENOTTY || err == -EINVAL) {
214 pr_warn_once("copying fileattr: no support on upper\n");
215 return 0;
216 }
5b0a414d 217 pr_warn("failed to retrieve upper fileattr (%pd2, err=%i)\n",
4ee7e4a6 218 new->dentry, err);
5b0a414d
MS
219 return err;
220 }
221
72db8211
AG
222 BUILD_BUG_ON(OVL_COPY_FS_FLAGS_MASK & ~FS_COMMON_FL);
223 newfa.flags &= ~OVL_COPY_FS_FLAGS_MASK;
224 newfa.flags |= (oldfa.flags & OVL_COPY_FS_FLAGS_MASK);
225
226 BUILD_BUG_ON(OVL_COPY_FSX_FLAGS_MASK & ~FS_XFLAG_COMMON);
227 newfa.fsx_xflags &= ~OVL_COPY_FSX_FLAGS_MASK;
228 newfa.fsx_xflags |= (oldfa.fsx_xflags & OVL_COPY_FSX_FLAGS_MASK);
229
230 return ovl_real_fileattr_set(new, &newfa);
231}
232
2b1a7746
MS
233static int ovl_copy_up_file(struct ovl_fs *ofs, struct dentry *dentry,
234 struct file *new_file, loff_t len)
e9be9d5e 235{
2b1a7746 236 struct path datapath;
e9be9d5e 237 struct file *old_file;
e9be9d5e
MS
238 loff_t old_pos = 0;
239 loff_t new_pos = 0;
42ec3d4c 240 loff_t cloned;
b504c654
CX
241 loff_t data_pos = -1;
242 loff_t hole_len;
243 bool skip_hole = false;
e9be9d5e
MS
244 int error = 0;
245
2b1a7746
MS
246 ovl_path_lowerdata(dentry, &datapath);
247 if (WARN_ON(datapath.dentry == NULL))
248 return -EIO;
e9be9d5e 249
2b1a7746 250 old_file = ovl_path_open(&datapath, O_LARGEFILE | O_RDONLY);
e9be9d5e
MS
251 if (IS_ERR(old_file))
252 return PTR_ERR(old_file);
253
2ea98466 254 /* Try to use clone_file_range to clone up within the same fs */
452ce659 255 cloned = do_clone_file_range(old_file, 0, new_file, 0, len, 0);
42ec3d4c 256 if (cloned == len)
2b1a7746 257 goto out_fput;
2ea98466 258 /* Couldn't clone, so now we try to copy the data */
2ea98466 259
b504c654 260 /* Check if lower fs supports seek operation */
4e3299ea 261 if (old_file->f_mode & FMODE_LSEEK)
b504c654
CX
262 skip_hole = true;
263
e9be9d5e
MS
264 while (len) {
265 size_t this_len = OVL_COPY_UP_CHUNK_SIZE;
266 long bytes;
267
268 if (len < this_len)
269 this_len = len;
270
271 if (signal_pending_state(TASK_KILLABLE, current)) {
272 error = -EINTR;
273 break;
274 }
275
b504c654
CX
276 /*
277 * Fill zero for hole will cost unnecessary disk space
278 * and meanwhile slow down the copy-up speed, so we do
279 * an optimization for hole during copy-up, it relies
280 * on SEEK_DATA implementation in lower fs so if lower
281 * fs does not support it, copy-up will behave as before.
282 *
283 * Detail logic of hole detection as below:
284 * When we detect next data position is larger than current
285 * position we will skip that hole, otherwise we copy
286 * data in the size of OVL_COPY_UP_CHUNK_SIZE. Actually,
287 * it may not recognize all kind of holes and sometimes
288 * only skips partial of hole area. However, it will be
289 * enough for most of the use cases.
290 */
291
292 if (skip_hole && data_pos < old_pos) {
293 data_pos = vfs_llseek(old_file, old_pos, SEEK_DATA);
294 if (data_pos > old_pos) {
295 hole_len = data_pos - old_pos;
296 len -= hole_len;
297 old_pos = new_pos = data_pos;
298 continue;
299 } else if (data_pos == -ENXIO) {
300 break;
301 } else if (data_pos < 0) {
302 skip_hole = false;
303 }
304 }
305
e9be9d5e
MS
306 bytes = do_splice_direct(old_file, &old_pos,
307 new_file, &new_pos,
308 this_len, SPLICE_F_MOVE);
309 if (bytes <= 0) {
310 error = bytes;
311 break;
312 }
313 WARN_ON(old_pos != new_pos);
314
315 len -= bytes;
316 }
c86243b0 317 if (!error && ovl_should_sync(ofs))
641089c1 318 error = vfs_fsync(new_file, 0);
e9be9d5e
MS
319out_fput:
320 fput(old_file);
321 return error;
322}
323
5272eaf3
CB
324static int ovl_set_size(struct ovl_fs *ofs,
325 struct dentry *upperdentry, struct kstat *stat)
0c288874
VG
326{
327 struct iattr attr = {
328 .ia_valid = ATTR_SIZE,
329 .ia_size = stat->size,
330 };
331
a15506ea 332 return ovl_do_notify_change(ofs, upperdentry, &attr);
0c288874
VG
333}
334
5272eaf3
CB
335static int ovl_set_timestamps(struct ovl_fs *ofs, struct dentry *upperdentry,
336 struct kstat *stat)
e9be9d5e
MS
337{
338 struct iattr attr = {
339 .ia_valid =
340 ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET,
341 .ia_atime = stat->atime,
342 .ia_mtime = stat->mtime,
343 };
344
a15506ea 345 return ovl_do_notify_change(ofs, upperdentry, &attr);
e9be9d5e
MS
346}
347
5272eaf3
CB
348int ovl_set_attr(struct ovl_fs *ofs, struct dentry *upperdentry,
349 struct kstat *stat)
e9be9d5e
MS
350{
351 int err = 0;
352
353 if (!S_ISLNK(stat->mode)) {
354 struct iattr attr = {
355 .ia_valid = ATTR_MODE,
356 .ia_mode = stat->mode,
357 };
a15506ea 358 err = ovl_do_notify_change(ofs, upperdentry, &attr);
e9be9d5e
MS
359 }
360 if (!err) {
361 struct iattr attr = {
362 .ia_valid = ATTR_UID | ATTR_GID,
b27c82e1
CB
363 .ia_vfsuid = VFSUIDT_INIT(stat->uid),
364 .ia_vfsgid = VFSGIDT_INIT(stat->gid),
e9be9d5e 365 };
a15506ea 366 err = ovl_do_notify_change(ofs, upperdentry, &attr);
e9be9d5e
MS
367 }
368 if (!err)
5272eaf3 369 ovl_set_timestamps(ofs, upperdentry, stat);
e9be9d5e
MS
370
371 return err;
e9be9d5e
MS
372}
373
1cdb0cb6
PT
374struct ovl_fh *ovl_encode_real_fh(struct ovl_fs *ofs, struct dentry *real,
375 bool is_upper)
3a1e819b
AG
376{
377 struct ovl_fh *fh;
ec7bbb53 378 int fh_type, dwords;
3a1e819b 379 int buflen = MAX_HANDLE_SZ;
05122443 380 uuid_t *uuid = &real->d_sb->s_uuid;
ec7bbb53 381 int err;
3a1e819b 382
ec7bbb53
AG
383 /* Make sure the real fid stays 32bit aligned */
384 BUILD_BUG_ON(OVL_FH_FID_OFFSET % 4);
385 BUILD_BUG_ON(MAX_HANDLE_SZ + OVL_FH_FID_OFFSET > 255);
386
387 fh = kzalloc(buflen + OVL_FH_FID_OFFSET, GFP_KERNEL);
388 if (!fh)
3a1e819b
AG
389 return ERR_PTR(-ENOMEM);
390
391 /*
392 * We encode a non-connectable file handle for non-dir, because we
393 * only need to find the lower inode number and we don't want to pay
394 * the price or reconnecting the dentry.
395 */
396 dwords = buflen >> 2;
ec7bbb53 397 fh_type = exportfs_encode_fh(real, (void *)fh->fb.fid, &dwords, 0);
3a1e819b
AG
398 buflen = (dwords << 2);
399
ec7bbb53 400 err = -EIO;
3a1e819b
AG
401 if (WARN_ON(fh_type < 0) ||
402 WARN_ON(buflen > MAX_HANDLE_SZ) ||
403 WARN_ON(fh_type == FILEID_INVALID))
ec7bbb53 404 goto out_err;
3a1e819b 405
cbe7fba8
AG
406 fh->fb.version = OVL_FH_VERSION;
407 fh->fb.magic = OVL_FH_MAGIC;
408 fh->fb.type = fh_type;
409 fh->fb.flags = OVL_FH_FLAG_CPU_ENDIAN;
54fb347e
AG
410 /*
411 * When we will want to decode an overlay dentry from this handle
412 * and all layers are on the same fs, if we get a disconncted real
413 * dentry when we decode fid, the only way to tell if we should assign
414 * it to upperdentry or to lowerstack is by checking this flag.
415 */
416 if (is_upper)
cbe7fba8 417 fh->fb.flags |= OVL_FH_FLAG_PATH_UPPER;
ec7bbb53 418 fh->fb.len = sizeof(fh->fb) + buflen;
5830fb6b
PT
419 if (ofs->config.uuid)
420 fh->fb.uuid = *uuid;
3a1e819b 421
3a1e819b 422 return fh;
ec7bbb53
AG
423
424out_err:
425 kfree(fh);
426 return ERR_PTR(err);
3a1e819b
AG
427}
428
a0c236b1
AG
429int ovl_set_origin(struct ovl_fs *ofs, struct dentry *lower,
430 struct dentry *upper)
3a1e819b 431{
3a1e819b
AG
432 const struct ovl_fh *fh = NULL;
433 int err;
434
435 /*
436 * When lower layer doesn't support export operations store a 'null' fh,
437 * so we can use the overlay.origin xattr to distignuish between a copy
438 * up and a pure upper inode.
439 */
02bcd157 440 if (ovl_can_decode_fh(lower->d_sb)) {
1cdb0cb6 441 fh = ovl_encode_real_fh(ofs, lower, false);
3a1e819b
AG
442 if (IS_ERR(fh))
443 return PTR_ERR(fh);
444 }
445
6266d465
MS
446 /*
447 * Do not fail when upper doesn't support xattrs.
448 */
a0c236b1 449 err = ovl_check_setxattr(ofs, upper, OVL_XATTR_ORIGIN, fh->buf,
cbe7fba8 450 fh ? fh->fb.len : 0, 0);
3a1e819b
AG
451 kfree(fh);
452
6939f977
MS
453 /* Ignore -EPERM from setting "user.*" on symlink/special */
454 return err == -EPERM ? 0 : err;
3a1e819b
AG
455}
456
016b720f 457/* Store file handle of @upper dir in @index dir entry */
610afc0b
MS
458static int ovl_set_upper_fh(struct ovl_fs *ofs, struct dentry *upper,
459 struct dentry *index)
016b720f
AG
460{
461 const struct ovl_fh *fh;
462 int err;
463
1cdb0cb6 464 fh = ovl_encode_real_fh(ofs, upper, true);
016b720f
AG
465 if (IS_ERR(fh))
466 return PTR_ERR(fh);
467
c914c0e2 468 err = ovl_setxattr(ofs, index, OVL_XATTR_UPPER, fh->buf, fh->fb.len);
016b720f
AG
469
470 kfree(fh);
471 return err;
472}
473
474/*
475 * Create and install index entry.
476 *
477 * Caller must hold i_mutex on indexdir.
478 */
479static int ovl_create_index(struct dentry *dentry, struct dentry *origin,
480 struct dentry *upper)
481{
1cdb0cb6 482 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
016b720f
AG
483 struct dentry *indexdir = ovl_indexdir(dentry->d_sb);
484 struct inode *dir = d_inode(indexdir);
485 struct dentry *index = NULL;
486 struct dentry *temp = NULL;
487 struct qstr name = { };
488 int err;
489
490 /*
491 * For now this is only used for creating index entry for directories,
492 * because non-dir are copied up directly to index and then hardlinked
493 * to upper dir.
494 *
495 * TODO: implement create index for non-dir, so we can call it when
496 * encoding file handle for non-dir in case index does not exist.
497 */
498 if (WARN_ON(!d_is_dir(dentry)))
499 return -EIO;
500
501 /* Directory not expected to be indexed before copy up */
502 if (WARN_ON(ovl_test_flag(OVL_INDEX, d_inode(dentry))))
503 return -EIO;
504
1cdb0cb6 505 err = ovl_get_index_name(ofs, origin, &name);
016b720f
AG
506 if (err)
507 return err;
508
576bb263 509 temp = ovl_create_temp(ofs, indexdir, OVL_CATTR(S_IFDIR | 0));
b148cba4 510 err = PTR_ERR(temp);
016b720f 511 if (IS_ERR(temp))
b148cba4 512 goto free_name;
016b720f 513
1cdb0cb6 514 err = ovl_set_upper_fh(ofs, upper, temp);
016b720f 515 if (err)
b148cba4 516 goto out;
016b720f 517
22f289ce 518 index = ovl_lookup_upper(ofs, name.name, indexdir, name.len);
016b720f
AG
519 if (IS_ERR(index)) {
520 err = PTR_ERR(index);
521 } else {
576bb263 522 err = ovl_do_rename(ofs, dir, temp, dir, index, 0);
016b720f
AG
523 dput(index);
524 }
016b720f 525out:
b148cba4 526 if (err)
576bb263 527 ovl_cleanup(ofs, dir, temp);
016b720f 528 dput(temp);
b148cba4 529free_name:
016b720f
AG
530 kfree(name.name);
531 return err;
016b720f
AG
532}
533
f4439de1
AG
534struct ovl_copy_up_ctx {
535 struct dentry *parent;
536 struct dentry *dentry;
537 struct path lowerpath;
538 struct kstat stat;
539 struct kstat pstat;
540 const char *link;
541 struct dentry *destdir;
542 struct qstr destname;
543 struct dentry *workdir;
f4439de1 544 bool origin;
016b720f 545 bool indexed;
44d5bf10 546 bool metacopy;
f4439de1
AG
547};
548
549static int ovl_link_up(struct ovl_copy_up_ctx *c)
59be0971
AG
550{
551 int err;
552 struct dentry *upper;
f4439de1 553 struct dentry *upperdir = ovl_dentry_upper(c->parent);
576bb263 554 struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb);
59be0971
AG
555 struct inode *udir = d_inode(upperdir);
556
f4439de1
AG
557 /* Mark parent "impure" because it may now contain non-pure upper */
558 err = ovl_set_impure(c->parent, upperdir);
559 if (err)
560 return err;
561
562 err = ovl_set_nlink_lower(c->dentry);
5f8415d6
AG
563 if (err)
564 return err;
565
59be0971 566 inode_lock_nested(udir, I_MUTEX_PARENT);
22f289ce
CB
567 upper = ovl_lookup_upper(ofs, c->dentry->d_name.name, upperdir,
568 c->dentry->d_name.len);
59be0971
AG
569 err = PTR_ERR(upper);
570 if (!IS_ERR(upper)) {
576bb263 571 err = ovl_do_link(ofs, ovl_dentry_upper(c->dentry), udir, upper);
59be0971
AG
572 dput(upper);
573
f4439de1
AG
574 if (!err) {
575 /* Restore timestamps on parent (best effort) */
5272eaf3 576 ovl_set_timestamps(ofs, upperdir, &c->pstat);
f4439de1
AG
577 ovl_dentry_set_upper_alias(c->dentry);
578 }
59be0971
AG
579 }
580 inode_unlock(udir);
aa3ff3c1
AG
581 if (err)
582 return err;
583
584 err = ovl_set_nlink_upper(c->dentry);
59be0971
AG
585
586 return err;
587}
588
2b1a7746 589static int ovl_copy_up_data(struct ovl_copy_up_ctx *c, const struct path *temp)
7d90b853 590{
c86243b0 591 struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb);
2b1a7746 592 struct file *new_file;
7d90b853
MS
593 int err;
594
2b1a7746
MS
595 if (!S_ISREG(c->stat.mode) || c->metacopy || !c->stat.size)
596 return 0;
72db8211 597
2b1a7746
MS
598 new_file = ovl_path_open(temp, O_LARGEFILE | O_WRONLY);
599 if (IS_ERR(new_file))
600 return PTR_ERR(new_file);
72db8211 601
2b1a7746
MS
602 err = ovl_copy_up_file(ofs, c->dentry, new_file, c->stat.size);
603 fput(new_file);
604
605 return err;
606}
607
608static int ovl_copy_up_metadata(struct ovl_copy_up_ctx *c, struct dentry *temp)
609{
610 struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb);
611 struct inode *inode = d_inode(c->dentry);
612 struct path upperpath = { .mnt = ovl_upper_mnt(ofs), .dentry = temp };
613 int err;
5f32879e 614
dad7017a 615 err = ovl_copy_xattr(c->dentry->d_sb, &c->lowerpath, temp);
e9be9d5e 616 if (err)
02209d10 617 return err;
e9be9d5e 618
72db8211
AG
619 if (inode->i_flags & OVL_COPY_I_FLAGS_MASK) {
620 /*
621 * Copy the fileattr inode flags that are the source of already
622 * copied i_flags
623 */
096a218a 624 err = ovl_copy_fileattr(inode, &c->lowerpath, &upperpath);
72db8211
AG
625 if (err)
626 return err;
627 }
628
3a1e819b
AG
629 /*
630 * Store identifier of lower inode in upper inode xattr to
631 * allow lookup of the copy up origin inode.
fbaf94ee
MS
632 *
633 * Don't set origin when we are breaking the association with a lower
634 * hard link.
3a1e819b 635 */
59be0971 636 if (c->origin) {
a0c236b1 637 err = ovl_set_origin(ofs, c->lowerpath.dentry, temp);
fbaf94ee 638 if (err)
02209d10 639 return err;
fbaf94ee 640 }
3a1e819b 641
0c288874 642 if (c->metacopy) {
a0c236b1 643 err = ovl_check_setxattr(ofs, temp, OVL_XATTR_METACOPY,
0c288874
VG
644 NULL, 0, -EOPNOTSUPP);
645 if (err)
646 return err;
647 }
648
bd64e575 649 inode_lock(temp->d_inode);
b504c654 650 if (S_ISREG(c->stat.mode))
5272eaf3 651 err = ovl_set_size(ofs, temp, &c->stat);
0c288874 652 if (!err)
5272eaf3 653 err = ovl_set_attr(ofs, temp, &c->stat);
bd64e575
VG
654 inode_unlock(temp->d_inode);
655
656 return err;
02209d10
AG
657}
658
6b52243f
MS
659struct ovl_cu_creds {
660 const struct cred *old;
661 struct cred *new;
662};
663
664static int ovl_prep_cu_creds(struct dentry *dentry, struct ovl_cu_creds *cc)
b10cdcdc
AG
665{
666 int err;
b10cdcdc 667
6b52243f
MS
668 cc->old = cc->new = NULL;
669 err = security_inode_copy_up(dentry, &cc->new);
b10cdcdc 670 if (err < 0)
6b52243f 671 return err;
b10cdcdc 672
6b52243f
MS
673 if (cc->new)
674 cc->old = override_creds(cc->new);
b10cdcdc 675
6b52243f 676 return 0;
b10cdcdc
AG
677}
678
6b52243f 679static void ovl_revert_cu_creds(struct ovl_cu_creds *cc)
b10cdcdc 680{
6b52243f
MS
681 if (cc->new) {
682 revert_creds(cc->old);
683 put_cred(cc->new);
684 }
b10cdcdc
AG
685}
686
687/*
688 * Copyup using workdir to prepare temp file. Used when copying up directories,
689 * special files or when upper fs doesn't support O_TMPFILE.
690 */
691static int ovl_copy_up_workdir(struct ovl_copy_up_ctx *c)
02209d10 692{
576bb263 693 struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb);
b79e05aa 694 struct inode *inode;
6b52243f 695 struct inode *udir = d_inode(c->destdir), *wdir = d_inode(c->workdir);
2b1a7746 696 struct path path = { .mnt = ovl_upper_mnt(ofs) };
6b52243f
MS
697 struct dentry *temp, *upper;
698 struct ovl_cu_creds cc;
02209d10 699 int err;
6b52243f
MS
700 struct ovl_cattr cattr = {
701 /* Can't properly set mode on creation because of the umask */
702 .mode = c->stat.mode & S_IFMT,
703 .rdev = c->stat.rdev,
704 .link = c->link
705 };
02209d10 706
773cb4c5
AG
707 /* workdir and destdir could be the same when copying up to indexdir */
708 err = -EIO;
709 if (lock_rename(c->workdir, c->destdir) != NULL)
710 goto unlock;
b10cdcdc 711
6b52243f
MS
712 err = ovl_prep_cu_creds(c->dentry, &cc);
713 if (err)
714 goto unlock;
715
576bb263 716 temp = ovl_create_temp(ofs, c->workdir, &cattr);
6b52243f
MS
717 ovl_revert_cu_creds(&cc);
718
b10cdcdc 719 err = PTR_ERR(temp);
b148cba4 720 if (IS_ERR(temp))
b10cdcdc 721 goto unlock;
02209d10 722
2b1a7746
MS
723 /*
724 * Copy up data first and then xattrs. Writing data after
725 * xattrs will remove security.capability xattr automatically.
726 */
727 path.dentry = temp;
728 err = ovl_copy_up_data(c, &path);
729 if (err)
730 goto cleanup;
731
732 err = ovl_copy_up_metadata(c, temp);
02209d10 733 if (err)
b10cdcdc 734 goto cleanup;
02209d10 735
016b720f
AG
736 if (S_ISDIR(c->stat.mode) && c->indexed) {
737 err = ovl_create_index(c->dentry, c->lowerpath.dentry, temp);
738 if (err)
b10cdcdc 739 goto cleanup;
016b720f
AG
740 }
741
22f289ce
CB
742 upper = ovl_lookup_upper(ofs, c->destname.name, c->destdir,
743 c->destname.len);
6b52243f
MS
744 err = PTR_ERR(upper);
745 if (IS_ERR(upper))
746 goto cleanup;
747
576bb263 748 err = ovl_do_rename(ofs, wdir, temp, udir, upper, 0);
6b52243f 749 dput(upper);
e9be9d5e 750 if (err)
b10cdcdc 751 goto cleanup;
e9be9d5e 752
0c288874
VG
753 if (!c->metacopy)
754 ovl_set_upperdata(d_inode(c->dentry));
b79e05aa 755 inode = d_inode(c->dentry);
6b52243f 756 ovl_inode_update(inode, temp);
b79e05aa
AG
757 if (S_ISDIR(inode->i_mode))
758 ovl_set_flag(OVL_WHITEOUTS, inode);
b10cdcdc
AG
759unlock:
760 unlock_rename(c->workdir, c->destdir);
761
762 return err;
763
764cleanup:
576bb263 765 ovl_cleanup(ofs, wdir, temp);
6b52243f
MS
766 dput(temp);
767 goto unlock;
b10cdcdc
AG
768}
769
6b52243f
MS
770/* Copyup using O_TMPFILE which does not require cross dir locking */
771static int ovl_copy_up_tmpfile(struct ovl_copy_up_ctx *c)
b10cdcdc 772{
576bb263 773 struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb);
6b52243f
MS
774 struct inode *udir = d_inode(c->destdir);
775 struct dentry *temp, *upper;
2b1a7746 776 struct file *tmpfile;
6b52243f 777 struct ovl_cu_creds cc;
b10cdcdc 778 int err;
b10cdcdc 779
6b52243f
MS
780 err = ovl_prep_cu_creds(c->dentry, &cc);
781 if (err)
782 return err;
b10cdcdc 783
2b1a7746 784 tmpfile = ovl_do_tmpfile(ofs, c->workdir, c->stat.mode);
6b52243f 785 ovl_revert_cu_creds(&cc);
b10cdcdc 786
2b1a7746
MS
787 if (IS_ERR(tmpfile))
788 return PTR_ERR(tmpfile);
b10cdcdc 789
2b1a7746
MS
790 temp = tmpfile->f_path.dentry;
791 if (!c->metacopy && c->stat.size) {
792 err = ovl_copy_up_file(ofs, c->dentry, tmpfile, c->stat.size);
793 if (err)
baabaa50 794 goto out_fput;
2b1a7746
MS
795 }
796
797 err = ovl_copy_up_metadata(c, temp);
6b52243f 798 if (err)
2b1a7746 799 goto out_fput;
b10cdcdc
AG
800
801 inode_lock_nested(udir, I_MUTEX_PARENT);
802
22f289ce
CB
803 upper = ovl_lookup_upper(ofs, c->destname.name, c->destdir,
804 c->destname.len);
b10cdcdc 805 err = PTR_ERR(upper);
6b52243f 806 if (!IS_ERR(upper)) {
576bb263 807 err = ovl_do_link(ofs, temp, udir, upper);
6b52243f
MS
808 dput(upper);
809 }
b10cdcdc
AG
810 inode_unlock(udir);
811
b10cdcdc 812 if (err)
2b1a7746 813 goto out_fput;
b10cdcdc
AG
814
815 if (!c->metacopy)
816 ovl_set_upperdata(d_inode(c->dentry));
2b1a7746 817 ovl_inode_update(d_inode(c->dentry), dget(temp));
b79e05aa 818
2b1a7746
MS
819out_fput:
820 fput(tmpfile);
e9be9d5e 821 return err;
e9be9d5e
MS
822}
823
824/*
825 * Copy up a single dentry
826 *
a6c60655
MS
827 * All renames start with copy up of source if necessary. The actual
828 * rename will only proceed once the copy up was successful. Copy up uses
829 * upper parent i_mutex for exclusion. Since rename can change d_parent it
830 * is possible that the copy up will lock the old parent. At that point
831 * the file will have already been copied up anyway.
e9be9d5e 832 */
a6fb235a 833static int ovl_do_copy_up(struct ovl_copy_up_ctx *c)
e9be9d5e 834{
e9be9d5e 835 int err;
1cdb0cb6 836 struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb);
016b720f 837 bool to_index = false;
59be0971 838
016b720f
AG
839 /*
840 * Indexed non-dir is copied up directly to the index entry and then
841 * hardlinked to upper dir. Indexed dir is copied up to indexdir,
842 * then index entry is created and then copied up dir installed.
843 * Copying dir up to indexdir instead of workdir simplifies locking.
844 */
845 if (ovl_need_index(c->dentry)) {
846 c->indexed = true;
847 if (S_ISDIR(c->stat.mode))
848 c->workdir = ovl_indexdir(c->dentry->d_sb);
849 else
850 to_index = true;
851 }
852
853 if (S_ISDIR(c->stat.mode) || c->stat.nlink == 1 || to_index)
59be0971
AG
854 c->origin = true;
855
016b720f 856 if (to_index) {
59be0971 857 c->destdir = ovl_indexdir(c->dentry->d_sb);
1cdb0cb6 858 err = ovl_get_index_name(ofs, c->lowerpath.dentry, &c->destname);
59be0971
AG
859 if (err)
860 return err;
aa3ff3c1
AG
861 } else if (WARN_ON(!c->parent)) {
862 /* Disconnected dentry must be copied up to index dir */
863 return -EIO;
59be0971
AG
864 } else {
865 /*
866 * Mark parent "impure" because it may now contain non-pure
867 * upper
868 */
869 err = ovl_set_impure(c->parent, c->destdir);
870 if (err)
871 return err;
872 }
e9be9d5e 873
01ad3eb8 874 /* Should we copyup with O_TMPFILE or with workdir? */
b10cdcdc
AG
875 if (S_ISREG(c->stat.mode) && ofs->tmpfile)
876 err = ovl_copy_up_tmpfile(c);
877 else
878 err = ovl_copy_up_workdir(c);
aa3ff3c1
AG
879 if (err)
880 goto out;
881
882 if (c->indexed)
016b720f
AG
883 ovl_set_flag(OVL_INDEX, d_inode(c->dentry));
884
885 if (to_index) {
aa3ff3c1
AG
886 /* Initialize nlink for copy up of disconnected dentry */
887 err = ovl_set_nlink_upper(c->dentry);
888 } else {
59be0971
AG
889 struct inode *udir = d_inode(c->destdir);
890
891 /* Restore timestamps on parent (best effort) */
892 inode_lock(udir);
5272eaf3 893 ovl_set_timestamps(ofs, c->destdir, &c->pstat);
59be0971
AG
894 inode_unlock(udir);
895
896 ovl_dentry_set_upper_alias(c->dentry);
e9be9d5e
MS
897 }
898
aa3ff3c1
AG
899out:
900 if (to_index)
901 kfree(c->destname.name);
a6fb235a
MS
902 return err;
903}
904
44d5bf10
VG
905static bool ovl_need_meta_copy_up(struct dentry *dentry, umode_t mode,
906 int flags)
907{
908 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
909
44d5bf10
VG
910 if (!ofs->config.metacopy)
911 return false;
912
913 if (!S_ISREG(mode))
914 return false;
915
916 if (flags && ((OPEN_FMODE(flags) & FMODE_WRITE) || (flags & O_TRUNC)))
917 return false;
918
919 return true;
920}
921
2d343087 922static ssize_t ovl_getxattr_value(const struct path *path, char *name, char **value)
fee0f298
MS
923{
924 ssize_t res;
de7a52c9 925 char *buf;
fee0f298 926
dad7017a 927 res = ovl_do_getxattr(path, name, NULL, 0);
de7a52c9
MS
928 if (res == -ENODATA || res == -EOPNOTSUPP)
929 res = 0;
fee0f298 930
de7a52c9
MS
931 if (res > 0) {
932 buf = kzalloc(res, GFP_KERNEL);
fee0f298
MS
933 if (!buf)
934 return -ENOMEM;
935
dad7017a 936 res = ovl_do_getxattr(path, name, buf, res);
fee0f298 937 if (res < 0)
de7a52c9
MS
938 kfree(buf);
939 else
940 *value = buf;
fee0f298 941 }
fee0f298
MS
942 return res;
943}
944
0c288874
VG
945/* Copy up data of an inode which was copied up metadata only in the past. */
946static int ovl_copy_up_meta_inode_data(struct ovl_copy_up_ctx *c)
947{
c86243b0 948 struct ovl_fs *ofs = OVL_FS(c->dentry->d_sb);
2b1a7746 949 struct path upperpath;
0c288874 950 int err;
993a0b2a 951 char *capability = NULL;
3f649ab7 952 ssize_t cap_size;
0c288874
VG
953
954 ovl_path_upper(c->dentry, &upperpath);
955 if (WARN_ON(upperpath.dentry == NULL))
956 return -EIO;
957
993a0b2a 958 if (c->stat.size) {
dad7017a
CB
959 err = cap_size = ovl_getxattr_value(&upperpath, XATTR_NAME_CAPS,
960 &capability);
de7a52c9 961 if (cap_size < 0)
993a0b2a
VG
962 goto out;
963 }
964
2b1a7746 965 err = ovl_copy_up_data(c, &upperpath);
0c288874 966 if (err)
993a0b2a
VG
967 goto out_free;
968
969 /*
970 * Writing to upper file will clear security.capability xattr. We
971 * don't want that to happen for normal copy-up operation.
972 */
973 if (capability) {
c914c0e2
AG
974 err = ovl_do_setxattr(ofs, upperpath.dentry, XATTR_NAME_CAPS,
975 capability, cap_size, 0);
993a0b2a
VG
976 if (err)
977 goto out_free;
978 }
979
0c288874 980
c914c0e2 981 err = ovl_removexattr(ofs, upperpath.dentry, OVL_XATTR_METACOPY);
0c288874 982 if (err)
993a0b2a 983 goto out_free;
0c288874
VG
984
985 ovl_set_upperdata(d_inode(c->dentry));
993a0b2a
VG
986out_free:
987 kfree(capability);
988out:
0c288874
VG
989 return err;
990}
991
a6fb235a
MS
992static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
993 int flags)
994{
995 int err;
996 DEFINE_DELAYED_CALL(done);
997 struct path parentpath;
998 struct ovl_copy_up_ctx ctx = {
999 .parent = parent,
1000 .dentry = dentry,
1001 .workdir = ovl_workdir(dentry),
1002 };
1003
1004 if (WARN_ON(!ctx.workdir))
1005 return -EROFS;
1006
1007 ovl_path_lower(dentry, &ctx.lowerpath);
1008 err = vfs_getattr(&ctx.lowerpath, &ctx.stat,
1009 STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
1010 if (err)
1011 return err;
1012
4f11ada1
MS
1013 if (!kuid_has_mapping(current_user_ns(), ctx.stat.uid) ||
1014 !kgid_has_mapping(current_user_ns(), ctx.stat.gid))
1015 return -EOVERFLOW;
1016
44d5bf10
VG
1017 ctx.metacopy = ovl_need_meta_copy_up(dentry, ctx.stat.mode, flags);
1018
aa3ff3c1
AG
1019 if (parent) {
1020 ovl_path_upper(parent, &parentpath);
1021 ctx.destdir = parentpath.dentry;
1022 ctx.destname = dentry->d_name;
a6fb235a 1023
aa3ff3c1
AG
1024 err = vfs_getattr(&parentpath, &ctx.pstat,
1025 STATX_ATIME | STATX_MTIME,
1026 AT_STATX_SYNC_AS_STAT);
1027 if (err)
1028 return err;
1029 }
a6fb235a
MS
1030
1031 /* maybe truncate regular file. this has no effect on dirs */
1032 if (flags & O_TRUNC)
1033 ctx.stat.size = 0;
1034
1035 if (S_ISLNK(ctx.stat.mode)) {
1036 ctx.link = vfs_get_link(ctx.lowerpath.dentry, &done);
1037 if (IS_ERR(ctx.link))
1038 return PTR_ERR(ctx.link);
1039 }
a6fb235a 1040
0c288874 1041 err = ovl_copy_up_start(dentry, flags);
fd210b7d
MS
1042 /* err < 0: interrupted, err > 0: raced with another copy-up */
1043 if (unlikely(err)) {
1044 if (err > 0)
1045 err = 0;
1046 } else {
59be0971
AG
1047 if (!ovl_dentry_upper(dentry))
1048 err = ovl_do_copy_up(&ctx);
aa3ff3c1 1049 if (!err && parent && !ovl_dentry_has_upper_alias(dentry))
f4439de1 1050 err = ovl_link_up(&ctx);
0c288874
VG
1051 if (!err && ovl_dentry_needs_data_copy_up_locked(dentry, flags))
1052 err = ovl_copy_up_meta_inode_data(&ctx);
fd210b7d
MS
1053 ovl_copy_up_end(dentry);
1054 }
7764235b 1055 do_delayed_call(&done);
e9be9d5e
MS
1056
1057 return err;
1058}
1059
5ac8e802 1060static int ovl_copy_up_flags(struct dentry *dentry, int flags)
e9be9d5e 1061{
8eac98b8 1062 int err = 0;
7b279bbf 1063 const struct cred *old_cred;
aa3ff3c1
AG
1064 bool disconnected = (dentry->d_flags & DCACHE_DISCONNECTED);
1065
1066 /*
1067 * With NFS export, copy up can get called for a disconnected non-dir.
1068 * In this case, we will copy up lower inode to index dir without
1069 * linking it to upper dir.
1070 */
1071 if (WARN_ON(disconnected && d_is_dir(dentry)))
1072 return -EIO;
e9be9d5e 1073
7b279bbf 1074 old_cred = ovl_override_creds(dentry->d_sb);
e9be9d5e
MS
1075 while (!err) {
1076 struct dentry *next;
aa3ff3c1 1077 struct dentry *parent = NULL;
e9be9d5e 1078
0c288874 1079 if (ovl_already_copied_up(dentry, flags))
e9be9d5e
MS
1080 break;
1081
1082 next = dget(dentry);
1083 /* find the topmost dentry not yet copied up */
aa3ff3c1 1084 for (; !disconnected;) {
e9be9d5e
MS
1085 parent = dget_parent(next);
1086
59be0971 1087 if (ovl_dentry_upper(parent))
e9be9d5e
MS
1088 break;
1089
1090 dput(next);
1091 next = parent;
1092 }
1093
a6fb235a 1094 err = ovl_copy_up_one(parent, next, flags);
e9be9d5e
MS
1095
1096 dput(parent);
1097 dput(next);
1098 }
8eac98b8 1099 revert_creds(old_cred);
e9be9d5e
MS
1100
1101 return err;
1102}
9aba6521 1103
d6eac039
VG
1104static bool ovl_open_need_copy_up(struct dentry *dentry, int flags)
1105{
1106 /* Copy up of disconnected dentry does not set upper alias */
0c288874 1107 if (ovl_already_copied_up(dentry, flags))
d6eac039
VG
1108 return false;
1109
1110 if (special_file(d_inode(dentry)->i_mode))
1111 return false;
1112
0c288874 1113 if (!ovl_open_flags_need_copy_up(flags))
d6eac039
VG
1114 return false;
1115
1116 return true;
1117}
1118
3428030d 1119int ovl_maybe_copy_up(struct dentry *dentry, int flags)
d6eac039
VG
1120{
1121 int err = 0;
1122
3428030d 1123 if (ovl_open_need_copy_up(dentry, flags)) {
d6eac039
VG
1124 err = ovl_want_write(dentry);
1125 if (!err) {
3428030d 1126 err = ovl_copy_up_flags(dentry, flags);
d6eac039
VG
1127 ovl_drop_write(dentry);
1128 }
1129 }
1130
1131 return err;
1132}
1133
d1e6f6a9
VG
1134int ovl_copy_up_with_data(struct dentry *dentry)
1135{
1136 return ovl_copy_up_flags(dentry, O_WRONLY);
1137}
1138
9aba6521
AG
1139int ovl_copy_up(struct dentry *dentry)
1140{
1141 return ovl_copy_up_flags(dentry, 0);
1142}