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