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