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