]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - rpmsg/rpmsg.git/blob - fs/overlayfs/copy_up.c
Merge tag 'kbuild-fixes-v4.19-2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[rpmsg/rpmsg.git] / fs / overlayfs / copy_up.c
1 /*
2  *
3  * Copyright (C) 2011 Novell Inc.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 as published by
7  * the Free Software Foundation.
8  */
10 #include <linux/module.h>
11 #include <linux/fs.h>
12 #include <linux/slab.h>
13 #include <linux/file.h>
14 #include <linux/splice.h>
15 #include <linux/xattr.h>
16 #include <linux/security.h>
17 #include <linux/uaccess.h>
18 #include <linux/sched/signal.h>
19 #include <linux/cred.h>
20 #include <linux/namei.h>
21 #include <linux/fdtable.h>
22 #include <linux/ratelimit.h>
23 #include <linux/exportfs.h>
24 #include "overlayfs.h"
26 #define OVL_COPY_UP_CHUNK_SIZE (1 << 20)
28 static int ovl_ccup_set(const char *buf, const struct kernel_param *param)
29 {
30         pr_warn("overlayfs: \"check_copy_up\" module option is obsolete\n");
31         return 0;
32 }
34 static int ovl_ccup_get(char *buf, const struct kernel_param *param)
35 {
36         return sprintf(buf, "N\n");
37 }
39 module_param_call(check_copy_up, ovl_ccup_set, ovl_ccup_get, NULL, 0644);
40 MODULE_PARM_DESC(ovl_check_copy_up, "Obsolete; does nothing");
42 int ovl_copy_xattr(struct dentry *old, struct dentry *new)
43 {
44         ssize_t list_size, size, value_size = 0;
45         char *buf, *name, *value = NULL;
46         int uninitialized_var(error);
47         size_t slen;
49         if (!(old->d_inode->i_opflags & IOP_XATTR) ||
50             !(new->d_inode->i_opflags & IOP_XATTR))
51                 return 0;
53         list_size = vfs_listxattr(old, NULL, 0);
54         if (list_size <= 0) {
55                 if (list_size == -EOPNOTSUPP)
56                         return 0;
57                 return list_size;
58         }
60         buf = kzalloc(list_size, GFP_KERNEL);
61         if (!buf)
62                 return -ENOMEM;
64         list_size = vfs_listxattr(old, buf, list_size);
65         if (list_size <= 0) {
66                 error = list_size;
67                 goto out;
68         }
70         for (name = buf; list_size; name += slen) {
71                 slen = strnlen(name, list_size) + 1;
73                 /* underlying fs providing us with an broken xattr list? */
74                 if (WARN_ON(slen > list_size)) {
75                         error = -EIO;
76                         break;
77                 }
78                 list_size -= slen;
80                 if (ovl_is_private_xattr(name))
81                         continue;
82 retry:
83                 size = vfs_getxattr(old, name, value, value_size);
84                 if (size == -ERANGE)
85                         size = vfs_getxattr(old, name, NULL, 0);
87                 if (size < 0) {
88                         error = size;
89                         break;
90                 }
92                 if (size > value_size) {
93                         void *new;
95                         new = krealloc(value, size, GFP_KERNEL);
96                         if (!new) {
97                                 error = -ENOMEM;
98                                 break;
99                         }
100                         value = new;
101                         value_size = size;
102                         goto retry;
103                 }
105                 error = security_inode_copy_up_xattr(name);
106                 if (error < 0 && error != -EOPNOTSUPP)
107                         break;
108                 if (error == 1) {
109                         error = 0;
110                         continue; /* Discard */
111                 }
112                 error = vfs_setxattr(new, name, value, size, 0);
113                 if (error)
114                         break;
115         }
116         kfree(value);
117 out:
118         kfree(buf);
119         return error;
122 static int ovl_copy_up_data(struct path *old, struct path *new, loff_t len)
124         struct file *old_file;
125         struct file *new_file;
126         loff_t old_pos = 0;
127         loff_t new_pos = 0;
128         int error = 0;
130         if (len == 0)
131                 return 0;
133         old_file = ovl_path_open(old, O_LARGEFILE | O_RDONLY);
134         if (IS_ERR(old_file))
135                 return PTR_ERR(old_file);
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         }
143         /* Try to use clone_file_range to clone up within the same fs */
144         error = do_clone_file_range(old_file, 0, new_file, 0, len);
145         if (!error)
146                 goto out;
147         /* Couldn't clone, so now we try to copy the data */
148         error = 0;
150         /* FIXME: copy up sparse files efficiently */
151         while (len) {
152                 size_t this_len = OVL_COPY_UP_CHUNK_SIZE;
153                 long bytes;
155                 if (len < this_len)
156                         this_len = len;
158                 if (signal_pending_state(TASK_KILLABLE, current)) {
159                         error = -EINTR;
160                         break;
161                 }
163                 bytes = do_splice_direct(old_file, &old_pos,
164                                          new_file, &new_pos,
165                                          this_len, SPLICE_F_MOVE);
166                 if (bytes <= 0) {
167                         error = bytes;
168                         break;
169                 }
170                 WARN_ON(old_pos != new_pos);
172                 len -= bytes;
173         }
174 out:
175         if (!error)
176                 error = vfs_fsync(new_file, 0);
177         fput(new_file);
178 out_fput:
179         fput(old_file);
180         return error;
183 static int ovl_set_size(struct dentry *upperdentry, struct kstat *stat)
185         struct iattr attr = {
186                 .ia_valid = ATTR_SIZE,
187                 .ia_size = stat->size,
188         };
190         return notify_change(upperdentry, &attr, NULL);
193 static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat)
195         struct iattr attr = {
196                 .ia_valid =
197                      ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET,
198                 .ia_atime = stat->atime,
199                 .ia_mtime = stat->mtime,
200         };
202         return notify_change(upperdentry, &attr, NULL);
205 int ovl_set_attr(struct dentry *upperdentry, struct kstat *stat)
207         int err = 0;
209         if (!S_ISLNK(stat->mode)) {
210                 struct iattr attr = {
211                         .ia_valid = ATTR_MODE,
212                         .ia_mode = stat->mode,
213                 };
214                 err = notify_change(upperdentry, &attr, NULL);
215         }
216         if (!err) {
217                 struct iattr attr = {
218                         .ia_valid = ATTR_UID | ATTR_GID,
219                         .ia_uid = stat->uid,
220                         .ia_gid = stat->gid,
221                 };
222                 err = notify_change(upperdentry, &attr, NULL);
223         }
224         if (!err)
225                 ovl_set_timestamps(upperdentry, stat);
227         return err;
230 struct ovl_fh *ovl_encode_real_fh(struct dentry *real, bool is_upper)
232         struct ovl_fh *fh;
233         int fh_type, fh_len, dwords;
234         void *buf;
235         int buflen = MAX_HANDLE_SZ;
236         uuid_t *uuid = &real->d_sb->s_uuid;
238         buf = kmalloc(buflen, GFP_KERNEL);
239         if (!buf)
240                 return ERR_PTR(-ENOMEM);
242         /*
243          * We encode a non-connectable file handle for non-dir, because we
244          * only need to find the lower inode number and we don't want to pay
245          * the price or reconnecting the dentry.
246          */
247         dwords = buflen >> 2;
248         fh_type = exportfs_encode_fh(real, buf, &dwords, 0);
249         buflen = (dwords << 2);
251         fh = ERR_PTR(-EIO);
252         if (WARN_ON(fh_type < 0) ||
253             WARN_ON(buflen > MAX_HANDLE_SZ) ||
254             WARN_ON(fh_type == FILEID_INVALID))
255                 goto out;
257         BUILD_BUG_ON(MAX_HANDLE_SZ + offsetof(struct ovl_fh, fid) > 255);
258         fh_len = offsetof(struct ovl_fh, fid) + buflen;
259         fh = kmalloc(fh_len, GFP_KERNEL);
260         if (!fh) {
261                 fh = ERR_PTR(-ENOMEM);
262                 goto out;
263         }
265         fh->version = OVL_FH_VERSION;
266         fh->magic = OVL_FH_MAGIC;
267         fh->type = fh_type;
268         fh->flags = OVL_FH_FLAG_CPU_ENDIAN;
269         /*
270          * When we will want to decode an overlay dentry from this handle
271          * and all layers are on the same fs, if we get a disconncted real
272          * dentry when we decode fid, the only way to tell if we should assign
273          * it to upperdentry or to lowerstack is by checking this flag.
274          */
275         if (is_upper)
276                 fh->flags |= OVL_FH_FLAG_PATH_UPPER;
277         fh->len = fh_len;
278         fh->uuid = *uuid;
279         memcpy(fh->fid, buf, buflen);
281 out:
282         kfree(buf);
283         return fh;
286 int ovl_set_origin(struct dentry *dentry, struct dentry *lower,
287                    struct dentry *upper)
289         const struct ovl_fh *fh = NULL;
290         int err;
292         /*
293          * When lower layer doesn't support export operations store a 'null' fh,
294          * so we can use the overlay.origin xattr to distignuish between a copy
295          * up and a pure upper inode.
296          */
297         if (ovl_can_decode_fh(lower->d_sb)) {
298                 fh = ovl_encode_real_fh(lower, false);
299                 if (IS_ERR(fh))
300                         return PTR_ERR(fh);
301         }
303         /*
304          * Do not fail when upper doesn't support xattrs.
305          */
306         err = ovl_check_setxattr(dentry, upper, OVL_XATTR_ORIGIN, fh,
307                                  fh ? fh->len : 0, 0);
308         kfree(fh);
310         return err;
313 /* Store file handle of @upper dir in @index dir entry */
314 static int ovl_set_upper_fh(struct dentry *upper, struct dentry *index)
316         const struct ovl_fh *fh;
317         int err;
319         fh = ovl_encode_real_fh(upper, true);
320         if (IS_ERR(fh))
321                 return PTR_ERR(fh);
323         err = ovl_do_setxattr(index, OVL_XATTR_UPPER, fh, fh->len, 0);
325         kfree(fh);
326         return err;
329 /*
330  * Create and install index entry.
331  *
332  * Caller must hold i_mutex on indexdir.
333  */
334 static int ovl_create_index(struct dentry *dentry, struct dentry *origin,
335                             struct dentry *upper)
337         struct dentry *indexdir = ovl_indexdir(dentry->d_sb);
338         struct inode *dir = d_inode(indexdir);
339         struct dentry *index = NULL;
340         struct dentry *temp = NULL;
341         struct qstr name = { };
342         int err;
344         /*
345          * For now this is only used for creating index entry for directories,
346          * because non-dir are copied up directly to index and then hardlinked
347          * to upper dir.
348          *
349          * TODO: implement create index for non-dir, so we can call it when
350          * encoding file handle for non-dir in case index does not exist.
351          */
352         if (WARN_ON(!d_is_dir(dentry)))
353                 return -EIO;
355         /* Directory not expected to be indexed before copy up */
356         if (WARN_ON(ovl_test_flag(OVL_INDEX, d_inode(dentry))))
357                 return -EIO;
359         err = ovl_get_index_name(origin, &name);
360         if (err)
361                 return err;
363         temp = ovl_create_temp(indexdir, OVL_CATTR(S_IFDIR | 0));
364         err = PTR_ERR(temp);
365         if (IS_ERR(temp))
366                 goto free_name;
368         err = ovl_set_upper_fh(upper, temp);
369         if (err)
370                 goto out;
372         index = lookup_one_len(name.name, indexdir, name.len);
373         if (IS_ERR(index)) {
374                 err = PTR_ERR(index);
375         } else {
376                 err = ovl_do_rename(dir, temp, dir, index, 0);
377                 dput(index);
378         }
379 out:
380         if (err)
381                 ovl_cleanup(dir, temp);
382         dput(temp);
383 free_name:
384         kfree(name.name);
385         return err;
388 struct ovl_copy_up_ctx {
389         struct dentry *parent;
390         struct dentry *dentry;
391         struct path lowerpath;
392         struct kstat stat;
393         struct kstat pstat;
394         const char *link;
395         struct dentry *destdir;
396         struct qstr destname;
397         struct dentry *workdir;
398         bool tmpfile;
399         bool origin;
400         bool indexed;
401         bool metacopy;
402 };
404 static int ovl_link_up(struct ovl_copy_up_ctx *c)
406         int err;
407         struct dentry *upper;
408         struct dentry *upperdir = ovl_dentry_upper(c->parent);
409         struct inode *udir = d_inode(upperdir);
411         /* Mark parent "impure" because it may now contain non-pure upper */
412         err = ovl_set_impure(c->parent, upperdir);
413         if (err)
414                 return err;
416         err = ovl_set_nlink_lower(c->dentry);
417         if (err)
418                 return err;
420         inode_lock_nested(udir, I_MUTEX_PARENT);
421         upper = lookup_one_len(c->dentry->d_name.name, upperdir,
422                                c->dentry->d_name.len);
423         err = PTR_ERR(upper);
424         if (!IS_ERR(upper)) {
425                 err = ovl_do_link(ovl_dentry_upper(c->dentry), udir, upper);
426                 dput(upper);
428                 if (!err) {
429                         /* Restore timestamps on parent (best effort) */
430                         ovl_set_timestamps(upperdir, &c->pstat);
431                         ovl_dentry_set_upper_alias(c->dentry);
432                 }
433         }
434         inode_unlock(udir);
435         if (err)
436                 return err;
438         err = ovl_set_nlink_upper(c->dentry);
440         return err;
443 static int ovl_install_temp(struct ovl_copy_up_ctx *c, struct dentry *temp,
444                             struct dentry **newdentry)
446         int err;
447         struct dentry *upper;
448         struct inode *udir = d_inode(c->destdir);
450         upper = lookup_one_len(c->destname.name, c->destdir, c->destname.len);
451         if (IS_ERR(upper))
452                 return PTR_ERR(upper);
454         if (c->tmpfile)
455                 err = ovl_do_link(temp, udir, upper);
456         else
457                 err = ovl_do_rename(d_inode(c->workdir), temp, udir, upper, 0);
459         if (!err)
460                 *newdentry = dget(c->tmpfile ? upper : temp);
461         dput(upper);
463         return err;
466 static struct dentry *ovl_get_tmpfile(struct ovl_copy_up_ctx *c)
468         int err;
469         struct dentry *temp;
470         const struct cred *old_creds = NULL;
471         struct cred *new_creds = NULL;
472         struct ovl_cattr cattr = {
473                 /* Can't properly set mode on creation because of the umask */
474                 .mode = c->stat.mode & S_IFMT,
475                 .rdev = c->stat.rdev,
476                 .link = c->link
477         };
479         err = security_inode_copy_up(c->dentry, &new_creds);
480         temp = ERR_PTR(err);
481         if (err < 0)
482                 goto out;
484         if (new_creds)
485                 old_creds = override_creds(new_creds);
487         if (c->tmpfile)
488                 temp = ovl_do_tmpfile(c->workdir, c->stat.mode);
489         else
490                 temp = ovl_create_temp(c->workdir, &cattr);
491 out:
492         if (new_creds) {
493                 revert_creds(old_creds);
494                 put_cred(new_creds);
495         }
497         return temp;
500 static int ovl_copy_up_inode(struct ovl_copy_up_ctx *c, struct dentry *temp)
502         int err;
504         err = ovl_copy_xattr(c->lowerpath.dentry, temp);
505         if (err)
506                 return err;
508         /*
509          * Store identifier of lower inode in upper inode xattr to
510          * allow lookup of the copy up origin inode.
511          *
512          * Don't set origin when we are breaking the association with a lower
513          * hard link.
514          */
515         if (c->origin) {
516                 err = ovl_set_origin(c->dentry, c->lowerpath.dentry, temp);
517                 if (err)
518                         return err;
519         }
521         if (S_ISREG(c->stat.mode) && !c->metacopy) {
522                 struct path upperpath, datapath;
524                 ovl_path_upper(c->dentry, &upperpath);
525                 BUG_ON(upperpath.dentry != NULL);
526                 upperpath.dentry = temp;
528                 ovl_path_lowerdata(c->dentry, &datapath);
529                 err = ovl_copy_up_data(&datapath, &upperpath, c->stat.size);
530                 if (err)
531                         return err;
532         }
534         if (c->metacopy) {
535                 err = ovl_check_setxattr(c->dentry, temp, OVL_XATTR_METACOPY,
536                                          NULL, 0, -EOPNOTSUPP);
537                 if (err)
538                         return err;
539         }
541         inode_lock(temp->d_inode);
542         if (c->metacopy)
543                 err = ovl_set_size(temp, &c->stat);
544         if (!err)
545                 err = ovl_set_attr(temp, &c->stat);
546         inode_unlock(temp->d_inode);
548         return err;
551 static int ovl_copy_up_locked(struct ovl_copy_up_ctx *c)
553         struct inode *udir = c->destdir->d_inode;
554         struct inode *inode;
555         struct dentry *newdentry = NULL;
556         struct dentry *temp;
557         int err;
559         temp = ovl_get_tmpfile(c);
560         if (IS_ERR(temp))
561                 return PTR_ERR(temp);
563         err = ovl_copy_up_inode(c, temp);
564         if (err)
565                 goto out;
567         if (S_ISDIR(c->stat.mode) && c->indexed) {
568                 err = ovl_create_index(c->dentry, c->lowerpath.dentry, temp);
569                 if (err)
570                         goto out;
571         }
573         if (c->tmpfile) {
574                 inode_lock_nested(udir, I_MUTEX_PARENT);
575                 err = ovl_install_temp(c, temp, &newdentry);
576                 inode_unlock(udir);
577         } else {
578                 err = ovl_install_temp(c, temp, &newdentry);
579         }
580         if (err)
581                 goto out;
583         if (!c->metacopy)
584                 ovl_set_upperdata(d_inode(c->dentry));
585         inode = d_inode(c->dentry);
586         ovl_inode_update(inode, newdentry);
587         if (S_ISDIR(inode->i_mode))
588                 ovl_set_flag(OVL_WHITEOUTS, inode);
590 out:
591         if (err && !c->tmpfile)
592                 ovl_cleanup(d_inode(c->workdir), temp);
593         dput(temp);
594         return err;
598 /*
599  * Copy up a single dentry
600  *
601  * All renames start with copy up of source if necessary.  The actual
602  * rename will only proceed once the copy up was successful.  Copy up uses
603  * upper parent i_mutex for exclusion.  Since rename can change d_parent it
604  * is possible that the copy up will lock the old parent.  At that point
605  * the file will have already been copied up anyway.
606  */
607 static int ovl_do_copy_up(struct ovl_copy_up_ctx *c)
609         int err;
610         struct ovl_fs *ofs = c->dentry->d_sb->s_fs_info;
611         bool to_index = false;
613         /*
614          * Indexed non-dir is copied up directly to the index entry and then
615          * hardlinked to upper dir. Indexed dir is copied up to indexdir,
616          * then index entry is created and then copied up dir installed.
617          * Copying dir up to indexdir instead of workdir simplifies locking.
618          */
619         if (ovl_need_index(c->dentry)) {
620                 c->indexed = true;
621                 if (S_ISDIR(c->stat.mode))
622                         c->workdir = ovl_indexdir(c->dentry->d_sb);
623                 else
624                         to_index = true;
625         }
627         if (S_ISDIR(c->stat.mode) || c->stat.nlink == 1 || to_index)
628                 c->origin = true;
630         if (to_index) {
631                 c->destdir = ovl_indexdir(c->dentry->d_sb);
632                 err = ovl_get_index_name(c->lowerpath.dentry, &c->destname);
633                 if (err)
634                         return err;
635         } else if (WARN_ON(!c->parent)) {
636                 /* Disconnected dentry must be copied up to index dir */
637                 return -EIO;
638         } else {
639                 /*
640                  * Mark parent "impure" because it may now contain non-pure
641                  * upper
642                  */
643                 err = ovl_set_impure(c->parent, c->destdir);
644                 if (err)
645                         return err;
646         }
648         /* Should we copyup with O_TMPFILE or with workdir? */
649         if (S_ISREG(c->stat.mode) && ofs->tmpfile) {
650                 c->tmpfile = true;
651                 err = ovl_copy_up_locked(c);
652         } else {
653                 err = ovl_lock_rename_workdir(c->workdir, c->destdir);
654                 if (!err) {
655                         err = ovl_copy_up_locked(c);
656                         unlock_rename(c->workdir, c->destdir);
657                 }
658         }
661         if (err)
662                 goto out;
664         if (c->indexed)
665                 ovl_set_flag(OVL_INDEX, d_inode(c->dentry));
667         if (to_index) {
668                 /* Initialize nlink for copy up of disconnected dentry */
669                 err = ovl_set_nlink_upper(c->dentry);
670         } else {
671                 struct inode *udir = d_inode(c->destdir);
673                 /* Restore timestamps on parent (best effort) */
674                 inode_lock(udir);
675                 ovl_set_timestamps(c->destdir, &c->pstat);
676                 inode_unlock(udir);
678                 ovl_dentry_set_upper_alias(c->dentry);
679         }
681 out:
682         if (to_index)
683                 kfree(c->destname.name);
684         return err;
687 static bool ovl_need_meta_copy_up(struct dentry *dentry, umode_t mode,
688                                   int flags)
690         struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
692         if (!ofs->config.metacopy)
693                 return false;
695         if (!S_ISREG(mode))
696                 return false;
698         if (flags && ((OPEN_FMODE(flags) & FMODE_WRITE) || (flags & O_TRUNC)))
699                 return false;
701         return true;
704 /* Copy up data of an inode which was copied up metadata only in the past. */
705 static int ovl_copy_up_meta_inode_data(struct ovl_copy_up_ctx *c)
707         struct path upperpath, datapath;
708         int err;
710         ovl_path_upper(c->dentry, &upperpath);
711         if (WARN_ON(upperpath.dentry == NULL))
712                 return -EIO;
714         ovl_path_lowerdata(c->dentry, &datapath);
715         if (WARN_ON(datapath.dentry == NULL))
716                 return -EIO;
718         err = ovl_copy_up_data(&datapath, &upperpath, c->stat.size);
719         if (err)
720                 return err;
722         err = vfs_removexattr(upperpath.dentry, OVL_XATTR_METACOPY);
723         if (err)
724                 return err;
726         ovl_set_upperdata(d_inode(c->dentry));
727         return err;
730 static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
731                            int flags)
733         int err;
734         DEFINE_DELAYED_CALL(done);
735         struct path parentpath;
736         struct ovl_copy_up_ctx ctx = {
737                 .parent = parent,
738                 .dentry = dentry,
739                 .workdir = ovl_workdir(dentry),
740         };
742         if (WARN_ON(!ctx.workdir))
743                 return -EROFS;
745         ovl_path_lower(dentry, &ctx.lowerpath);
746         err = vfs_getattr(&ctx.lowerpath, &ctx.stat,
747                           STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
748         if (err)
749                 return err;
751         ctx.metacopy = ovl_need_meta_copy_up(dentry, ctx.stat.mode, flags);
753         if (parent) {
754                 ovl_path_upper(parent, &parentpath);
755                 ctx.destdir = parentpath.dentry;
756                 ctx.destname = dentry->d_name;
758                 err = vfs_getattr(&parentpath, &ctx.pstat,
759                                   STATX_ATIME | STATX_MTIME,
760                                   AT_STATX_SYNC_AS_STAT);
761                 if (err)
762                         return err;
763         }
765         /* maybe truncate regular file. this has no effect on dirs */
766         if (flags & O_TRUNC)
767                 ctx.stat.size = 0;
769         if (S_ISLNK(ctx.stat.mode)) {
770                 ctx.link = vfs_get_link(ctx.lowerpath.dentry, &done);
771                 if (IS_ERR(ctx.link))
772                         return PTR_ERR(ctx.link);
773         }
775         err = ovl_copy_up_start(dentry, flags);
776         /* err < 0: interrupted, err > 0: raced with another copy-up */
777         if (unlikely(err)) {
778                 if (err > 0)
779                         err = 0;
780         } else {
781                 if (!ovl_dentry_upper(dentry))
782                         err = ovl_do_copy_up(&ctx);
783                 if (!err && parent && !ovl_dentry_has_upper_alias(dentry))
784                         err = ovl_link_up(&ctx);
785                 if (!err && ovl_dentry_needs_data_copy_up_locked(dentry, flags))
786                         err = ovl_copy_up_meta_inode_data(&ctx);
787                 ovl_copy_up_end(dentry);
788         }
789         do_delayed_call(&done);
791         return err;
794 int ovl_copy_up_flags(struct dentry *dentry, int flags)
796         int err = 0;
797         const struct cred *old_cred = ovl_override_creds(dentry->d_sb);
798         bool disconnected = (dentry->d_flags & DCACHE_DISCONNECTED);
800         /*
801          * With NFS export, copy up can get called for a disconnected non-dir.
802          * In this case, we will copy up lower inode to index dir without
803          * linking it to upper dir.
804          */
805         if (WARN_ON(disconnected && d_is_dir(dentry)))
806                 return -EIO;
808         while (!err) {
809                 struct dentry *next;
810                 struct dentry *parent = NULL;
812                 if (ovl_already_copied_up(dentry, flags))
813                         break;
815                 next = dget(dentry);
816                 /* find the topmost dentry not yet copied up */
817                 for (; !disconnected;) {
818                         parent = dget_parent(next);
820                         if (ovl_dentry_upper(parent))
821                                 break;
823                         dput(next);
824                         next = parent;
825                 }
827                 err = ovl_copy_up_one(parent, next, flags);
829                 dput(parent);
830                 dput(next);
831         }
832         revert_creds(old_cred);
834         return err;
837 static bool ovl_open_need_copy_up(struct dentry *dentry, int flags)
839         /* Copy up of disconnected dentry does not set upper alias */
840         if (ovl_already_copied_up(dentry, flags))
841                 return false;
843         if (special_file(d_inode(dentry)->i_mode))
844                 return false;
846         if (!ovl_open_flags_need_copy_up(flags))
847                 return false;
849         return true;
852 int ovl_open_maybe_copy_up(struct dentry *dentry, unsigned int file_flags)
854         int err = 0;
856         if (ovl_open_need_copy_up(dentry, file_flags)) {
857                 err = ovl_want_write(dentry);
858                 if (!err) {
859                         err = ovl_copy_up_flags(dentry, file_flags);
860                         ovl_drop_write(dentry);
861                 }
862         }
864         return err;
867 int ovl_copy_up_with_data(struct dentry *dentry)
869         return ovl_copy_up_flags(dentry, O_WRONLY);
872 int ovl_copy_up(struct dentry *dentry)
874         return ovl_copy_up_flags(dentry, 0);